diff --git a/.github/workflows/workflows.yml b/.github/workflows/workflows.yml new file mode 100644 index 0000000..f6d5a4b --- /dev/null +++ b/.github/workflows/workflows.yml @@ -0,0 +1,53 @@ +name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI + +on: + push: + tags: + - '*' + +jobs: + build: + name: Build distribution 📦 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Extract version from git tag + run: echo "PACKAGE_VERSION=${{ github.ref_name }}" >> $GITHUB_ENV + - name: Install pypa/build + run: python3 -m pip install build --user + - name: Build a binary wheel and a source tarball + run: python3 -m build + - name: Store the distribution packages + uses: actions/upload-artifact@v2 + with: + name: python-package-distributions + path: dist/ + + publish-to-pypi: + name: Publish Python 🐍 distribution 📦 to PyPI + if: startsWith(github.ref, 'refs/tags/') + needs: + - build + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/p/str2bool3 + permissions: + id-token: write + + steps: + - name: Download all the dists + uses: actions/download-artifact@v2 + with: + name: python-package-distributions + path: dist/ + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} + diff --git a/README.md b/README.md index 001f470..6b1433a 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,28 @@ -# str2bool v.1.1 +# str2bool3 v1.0.0 ## About -Convert string to boolean. -Library recognizes "yes", "true", "y", "t", "1" as True, and "no", "false", "n", "f", "0" as False. +Convert string to boolean. +The library recognizes "yes", "true", "y", "t", "1" as True, and "no", "false", "n", "f", "0" as False. Case insensitive. ## Installation - $ pip install str2bool + $ pip install str2bool3 ## Examples Here's a basic example: - >>> from str2bool import str2bool - >>> print(str2bool('Yes')) + >>> from str2bool3 import StrUtils + >>> print(StrUtils.str2bool('Yes')) True +To raise an exception on invalid input: + + >>> from str2bool3 import StrUtils + >>> StrUtils.str2bool_exc('invalid') + ValueError: Invalid value 'invalid'. Expected one of: false, f, n, no, t, true, y, yes, 0, 1. + ## License BSD + +Forked from symonsoft/str2bool diff --git a/setup.cfg b/setup.cfg index a2f3748..20ce750 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [metadata] -description-file = README.md \ No newline at end of file +description_file = README.md diff --git a/setup.py b/setup.py index 4687965..2a88dde 100644 --- a/setup.py +++ b/setup.py @@ -1,24 +1,32 @@ -from distutils.core import setup - +import os +from setuptools import setup +# Read version from environment variable, with fallback to default version +version = os.environ.get('PACKAGE_VERSION', '1.3.0') +with open("README.md", "r", encoding="utf-8") as f: + long_description = f.read() + 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', + name='str2bool3', + packages=['str2bool3'], + version=version, + description='Convert string to boolean (Forked from SymonSoft/str2bool)', + author='Sam Fakhreddine', + author_email='sam.fakhreddine@gmail.com', + url='https://github.com/sam-fakhreddine/str2bool3', + long_description=long_description, + long_description_content_type='text/markdown', + download_url=f'https://github.com/sam-fakhreddine/str2bool3/archive/refs/tags/{version}.tar.gz', 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', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', + 'Programming Language :: Python :: 3.12', 'Topic :: Utilities' ], ) diff --git a/str2bool/__init__.py b/str2bool/__init__.py deleted file mode 100644 index 4dc0017..0000000 --- a/str2bool/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -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) diff --git a/str2bool3/__init__.py b/str2bool3/__init__.py new file mode 100644 index 0000000..1b01fea --- /dev/null +++ b/str2bool3/__init__.py @@ -0,0 +1 @@ +from .str_utils import str2bool, str2bool_exc diff --git a/str2bool3/str_utils.py b/str2bool3/str_utils.py new file mode 100644 index 0000000..5c5a769 --- /dev/null +++ b/str2bool3/str_utils.py @@ -0,0 +1,23 @@ +from typing import Optional + +TRUE_SET = {'yes', 'true', 't', 'y', '1'} +FALSE_SET = {'no', 'false', 'f', 'n', '0'} + +def str2bool(value: Optional[str], raise_exc: bool = False, default: Optional[bool] = None) -> Optional[bool]: + if value is None: + return default + + value = value.lower() + if value in TRUE_SET: + return True + if value in FALSE_SET: + return False + + if raise_exc: + valid_values = ', '.join(sorted(TRUE_SET | FALSE_SET)) + raise ValueError(f"Invalid value '{value}'. Expected one of: {valid_values}.") + + return default + +def str2bool_exc(value: Optional[str]) -> bool: + return str2bool(value, raise_exc=True)