diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-01-04 11:37:42 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-01-04 11:37:42 -0600 |
| commit | 67b9067e24a92b0ca803912ac9aefa877ababcac (patch) | |
| tree | 3bd7cf65affa58abe08f48e99597a5b8389b2edd | |
| parent | Update changelog for 0.1.0 release and fix README extension in pyproject (diff) | |
| download | config-file-67b9067e24a92b0ca803912ac9aefa877ababcac.zip | |
Add ability to parse strings to their native values
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | config_file/parsers/__init__.py | 0 | ||||
| -rw-r--r-- | config_file/parsers/parse_value.py | 74 | ||||
| -rw-r--r-- | poetry.lock | 83 | ||||
| -rw-r--r-- | pyproject.toml | 2 | ||||
| -rw-r--r-- | tests/test_parse_value.py | 24 |
6 files changed, 178 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fff9dfe..035d765 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +Added + +- Parsing of strings to their native values + ## 0.1.0 - 2020-01-04 Added diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/config_file/parsers/__init__.py diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py new file mode 100644 index 0000000..8bde24a --- /dev/null +++ b/config_file/parsers/parse_value.py @@ -0,0 +1,74 @@ +""" +Parse types allows you to check or parse a string into its native type. + +Example: + parse_value('5') -> 5 (int) + parse_value('-5') -> -5 (int) + + parse_value('5.52') -> 5.52 (float) + parse_value('-53.2') -> -53.2 (float) + + parse_value('blahfafda') -> 'blahfafda' (string) + + parse_value('true') -> True (bool) + parse_value('FALSE') -> False (bool) + + +"5".isdigit() -> True +"-5".isdigit() -> False +can_be_parsed_as_int("-5") -> True +""" +import re +from distutils.util import strtobool + + +def parse_value(value): + if can_be_parsed_as_int(value): + return int(value) + + if can_be_parsed_as_float(value): + return float(value) + + if can_be_parsed_as_bool(value): + if type(value) is bool: + return bool(value) + + return bool(strtobool(value)) + + return value + + +def can_be_parsed_as_int(value) -> bool: + if type(value) is int: + return True + + if type(value) is not str: + return False + + if value.startswith("-"): + return value[1:].isdigit() + + return value.isdigit() + + +def can_be_parsed_as_float(value) -> bool: + if type(value) is float: + return True + + if type(value) is not str: + return False + + if value.startswith("-"): + return bool(re.match(r"^\d*\.\d+$", value[1:])) + + return bool(re.match(r"^\d*\.\d+$", value)) + + +def can_be_parsed_as_bool(value) -> bool: + if type(value) is bool: + return True + + if type(value) is not str: + return False + + return value.lower() == "true" or value.lower() == "false" diff --git a/poetry.lock b/poetry.lock index 47844ab..1b51a64 100644 --- a/poetry.lock +++ b/poetry.lock @@ -29,7 +29,7 @@ version = "1.3.0" pyyaml = "*" [[package]] -category = "dev" +category = "main" description = "Atomic file writes." name = "atomicwrites" optional = false @@ -37,7 +37,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" version = "1.3.0" [[package]] -category = "dev" +category = "main" description = "Classes Without Boilerplate" name = "attrs" optional = false @@ -114,7 +114,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" version = "7.0" [[package]] -category = "dev" +category = "main" description = "Cross-platform colored terminal text." marker = "sys_platform == \"win32\"" name = "colorama" @@ -156,6 +156,14 @@ pycodestyle = ">=2.5.0,<2.6.0" pyflakes = ">=2.1.0,<2.2.0" [[package]] +category = "main" +description = "Generate a dot graph from the output of several profilers." +name = "gprof2dot" +optional = false +python-versions = "*" +version = "2019.11.30" + +[[package]] category = "dev" description = "File identification library for Python" name = "identify" @@ -189,7 +197,7 @@ python-versions = "*" version = "0.6.1" [[package]] -category = "dev" +category = "main" description = "More routines for operating on iterables, beyond itertools" name = "more-itertools" optional = false @@ -205,7 +213,7 @@ python-versions = "*" version = "1.3.3" [[package]] -category = "dev" +category = "main" description = "plugin and hook calling mechanisms for python" name = "pluggy" optional = false @@ -234,7 +242,7 @@ toml = "*" virtualenv = ">=15.2" [[package]] -category = "dev" +category = "main" description = "library with cross-python path, ini-parsing, io, code, log facilities" name = "py" optional = false @@ -242,6 +250,14 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" version = "1.8.1" [[package]] +category = "main" +description = "Get CPU info with pure Python 2 & 3" +name = "py-cpuinfo" +optional = false +python-versions = "*" +version = "5.0.0" + +[[package]] category = "dev" description = "Python style guide checker" name = "pycodestyle" @@ -258,7 +274,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" version = "2.1.1" [[package]] -category = "dev" +category = "main" description = "pytest: simple powerful testing with Python" name = "pytest" optional = false @@ -276,6 +292,23 @@ setuptools = "*" six = ">=1.10.0" [[package]] +category = "main" +description = "A ``py.test`` fixture for benchmarking code. It will group the tests into rounds that are calibrated to the chosen timer. See calibration_ and FAQ_." +name = "pytest-benchmark" +optional = false +python-versions = "*" +version = "3.2.2" + +[package.dependencies] +py-cpuinfo = "*" +pytest = ">=3.8" + +[package.extras] +aspect = ["aspectlib"] +elasticsearch = ["elasticsearch"] +histogram = ["pygal", "pygaljs"] + +[[package]] category = "dev" description = "Pytest plugin for measuring coverage." name = "pytest-cov" @@ -292,6 +325,22 @@ testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "virtualenv"] [[package]] category = "main" +description = "Profiling plugin for py.test" +name = "pytest-profiling" +optional = false +python-versions = "*" +version = "1.7.0" + +[package.dependencies] +gprof2dot = "*" +pytest = "*" +six = "*" + +[package.extras] +tests = ["pytest-virtualenv"] + +[[package]] +category = "main" description = "YAML parser and emitter for Python" name = "pyyaml" optional = false @@ -310,7 +359,7 @@ version = "1.9.4" "aspy.refactor-imports" = "*" [[package]] -category = "dev" +category = "main" description = "Python 2 and 3 compatibility utilities" name = "six" optional = false @@ -338,7 +387,7 @@ docs = ["sphinx (>=1.8.0,<2)", "towncrier (>=18.5.0)", "sphinx-rtd-theme (>=0.4. testing = ["pytest (>=4.0.0,<5)", "coverage (>=4.5.0,<5)", "pytest-timeout (>=1.3.0,<2)", "six (>=1.10.0,<2)", "pytest-xdist", "pytest-localserver", "pypiserver", "mock", "xonsh"] [metadata] -content-hash = "9bb50501de7bb94f2eb9c0f65a9dd2eb9b748c8788a7a95ddd62cd033cc7ad6f" +content-hash = "28b82353ec2262fbee334fba084b04ea830ca71d604784565ea8d8c62b41d3f6" python-versions = "^3.8" [metadata.files] @@ -430,6 +479,9 @@ flake8 = [ {file = "flake8-3.7.9-py2.py3-none-any.whl", hash = "sha256:49356e766643ad15072a789a20915d3c91dc89fd313ccd71802303fd67e4deca"}, {file = "flake8-3.7.9.tar.gz", hash = "sha256:45681a117ecc81e870cbf1262835ae4af5e7a8b08e40b944a8a6e6b895914cfb"}, ] +gprof2dot = [ + {file = "gprof2dot-2019.11.30.tar.gz", hash = "sha256:b43fe04ebb3dfe181a612bbfc69e90555b8957022ad6a466f0308ed9c7f22e99"}, +] identify = [ {file = "identify-1.4.9-py2.py3-none-any.whl", hash = "sha256:72e9c4ed3bc713c7045b762b0d2e2115c572b85abfc1f4604f5a4fd4c6642b71"}, {file = "identify-1.4.9.tar.gz", hash = "sha256:6f44e637caa40d1b4cb37f6ed3b262ede74901d28b1cc5b1fc07360871edd65d"}, @@ -461,6 +513,9 @@ py = [ {file = "py-1.8.1-py2.py3-none-any.whl", hash = "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"}, {file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"}, ] +py-cpuinfo = [ + {file = "py-cpuinfo-5.0.0.tar.gz", hash = "sha256:2cf6426f776625b21d1db8397d3297ef7acfa59018f02a8779123f3190f18500"}, +] pycodestyle = [ {file = "pycodestyle-2.5.0-py2.py3-none-any.whl", hash = "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56"}, {file = "pycodestyle-2.5.0.tar.gz", hash = "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c"}, @@ -473,10 +528,20 @@ pytest = [ {file = "pytest-3.10.1-py2.py3-none-any.whl", hash = "sha256:3f193df1cfe1d1609d4c583838bea3d532b18d6160fd3f55c9447fdca30848ec"}, {file = "pytest-3.10.1.tar.gz", hash = "sha256:e246cf173c01169b9617fc07264b7b1316e78d7a650055235d6d897bc80d9660"}, ] +pytest-benchmark = [ + {file = "pytest-benchmark-3.2.2.tar.gz", hash = "sha256:4512c6805318d07926efcb3b39f7b98a10d035305a93edfd5329c86cbf9cfbf7"}, + {file = "pytest_benchmark-3.2.2-py2.py3-none-any.whl", hash = "sha256:ab851115ce022639173b9497d4a4183a1d8fe9cdcf8fab9d8a57607008aedd3d"}, +] pytest-cov = [ {file = "pytest-cov-2.8.1.tar.gz", hash = "sha256:cc6742d8bac45070217169f5f72ceee1e0e55b0221f54bcf24845972d3a47f2b"}, {file = "pytest_cov-2.8.1-py2.py3-none-any.whl", hash = "sha256:cdbdef4f870408ebdbfeb44e63e07eb18bb4619fae852f6e760645fa36172626"}, ] +pytest-profiling = [ + {file = "pytest-profiling-1.7.0.tar.gz", hash = "sha256:93938f147662225d2b8bd5af89587b979652426a8a6ffd7e73ec4a23e24b7f29"}, + {file = "pytest_profiling-1.7.0-py2.7.egg", hash = "sha256:3b255f9db36cb2dd7536a8e7e294c612c0be7f7850a7d30754878e4315d56600"}, + {file = "pytest_profiling-1.7.0-py2.py3-none-any.whl", hash = "sha256:999cc9ac94f2e528e3f5d43465da277429984a1c237ae9818f8cfd0b06acb019"}, + {file = "pytest_profiling-1.7.0-py3.6.egg", hash = "sha256:6bce4e2edc04409d2f3158c16750fab8074f62d404cc38eeb075dff7fcbb996c"}, +] pyyaml = [ {file = "PyYAML-5.2-cp27-cp27m-win32.whl", hash = "sha256:35ace9b4147848cafac3db142795ee42deebe9d0dad885ce643928e88daebdcc"}, {file = "PyYAML-5.2-cp27-cp27m-win_amd64.whl", hash = "sha256:ebc4ed52dcc93eeebeae5cf5deb2ae4347b3a81c3fa12b0b8c976544829396a4"}, diff --git a/pyproject.toml b/pyproject.toml index 846a225..018d928 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,8 @@ flake8 = "^3.7" bump2version = "^0.5.11" pytest = "^3.0" pytest-cov = "^2.8" +pytest-benchmark = "^3.2.2" +pytest-profiling = "^1.7.0" [tool.black] line-length = 88 diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py new file mode 100644 index 0000000..c3f00de --- /dev/null +++ b/tests/test_parse_value.py @@ -0,0 +1,24 @@ +import pytest + +from config_file.parsers.parse_value import parse_value + + +@pytest.mark.parametrize( + "test_input,expected", + [ + ("5", 5), + ("-5", -5), + ("-234.5", -234.5), + ("35.5", 35.5), + ("TRUE", True), + ("true", True), + ("false", False), + ("blah", "blah"), + (343, 343), + (3.2, 3.2), + (True, True), + ], +) +def test_parse_value(test_input, expected): + result = parse_value(test_input) + assert result == expected |