Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# str2bool v.1.1
# str2bool v.1.2

## About
Convert string to boolean.
Library recognizes "yes", "true", "y", "t", "1" as True, and "no", "false", "n", "f", "0" as False.
Library recognizes "yes", "true", "y", "t", "1", "enabled" as True, and "no", "false", "n", "f", "0", "disabled" as False.
Case insensitive.

## Installation
Expand Down
49 changes: 25 additions & 24 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from distutils.core import setup


setup(
name='str2bool',
packages=['str2bool'],
version='1.1',
description='Convert string to boolean',
author='SymonSoft',
author_email='symonsoft@gmail.com',
url='https://github.com/symonsoft/str2bool',
download_url='https://github.com/symonsoft/str2bool/tarball/1.1',
keywords=['str2bool', 'bool', 'boolean', 'convert', 'yes', 'no', 'true', 'false'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Topic :: Utilities'
],
)
from distutils.core import setup

version = "1.2"

setup(
name='str2bool',
packages=['str2bool'],
version=version,
description='Convert string to boolean',
author='SymonSoft',
author_email='symonsoft@gmail.com',
url='https://github.com/symonsoft/str2bool',
download_url="https://github.com/symonsoft/str2bool/tarball/{0}".format(version),
keywords=['str2bool', 'bool', 'boolean', 'convert', 'yes', 'no', 'true', 'false', 'enabled', 'disabled'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Topic :: Utilities'
],
)
41 changes: 20 additions & 21 deletions str2bool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import sys

_true_set = {'yes', 'true', 't', 'y', '1'}
_false_set = {'no', 'false', 'f', 'n', '0'}


def str2bool(value, raise_exc=False):
if isinstance(value, str) or sys.version_info[0] < 3 and isinstance(value, basestring):
value = value.lower()
if value in _true_set:
return True
if value in _false_set:
return False

if raise_exc:
raise ValueError('Expected "%s"' % '", "'.join(_true_set | _false_set))
return None


def str2bool_exc(value):
return str2bool(value, raise_exc=True)
import sys

_true_set = {'yes', 'true', 't', 'y', '1', 'enabled' }
_false_set = {'no', 'false', 'f', 'n', '0', 'disabled' }


def str2bool(value, raise_exc=False):
if isinstance(value, str) or sys.version_info[0] < 3 and isinstance(value, basestring):
value = value.lower()
if value in _true_set:
return True
if value in _false_set:
return False
if raise_exc:
raise ValueError('Expected "%s"' % '", "'.join(_true_set | _false_set))
return None


def str2bool_exc(value):
return str2bool(value, raise_exc=True)