From 77c5957ff336b2efaf874d80d4066db7476e0c40 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Mon, 30 Dec 2024 22:27:10 -0500 Subject: [PATCH 1/6] docs: generate docs with auto api --- doc/source/api/diffpy.utils.parsers.rst | 17 +++++++------ doc/source/api/diffpy.utils.rst | 33 ++++++++++++++++++------- doc/source/api/diffpy.utils.wx.rst | 1 + 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/doc/source/api/diffpy.utils.parsers.rst b/doc/source/api/diffpy.utils.parsers.rst index 3456f24e..b08a1840 100644 --- a/doc/source/api/diffpy.utils.parsers.rst +++ b/doc/source/api/diffpy.utils.parsers.rst @@ -11,14 +11,6 @@ diffpy.utils.parsers package Submodules ---------- -diffpy.utils.parsers.serialization module -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -.. automodule:: diffpy.utils.parsers.serialization - :members: - :undoc-members: - :show-inheritance: - diffpy.utils.parsers.loaddata module ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -34,3 +26,12 @@ diffpy.utils.parsers.custom_exceptions module :members: :undoc-members: :show-inheritance: + +diffpy.utils.parsers.serialization module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. automodule:: diffpy.utils.parsers.serialization + :members: + :undoc-members: + :show-inheritance: + diff --git a/doc/source/api/diffpy.utils.rst b/doc/source/api/diffpy.utils.rst index 2513e821..128d914d 100644 --- a/doc/source/api/diffpy.utils.rst +++ b/doc/source/api/diffpy.utils.rst @@ -16,30 +16,36 @@ Subpackages diffpy.utils.parsers diffpy.utils.wx - diffpy.utils.scattering_objects Submodules ---------- -diffpy.utils.tools module -^^^^^^^^^^^^^^^^^^^^^^^^^ +diffpy.utils.transforms module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. automodule:: diffpy.utils.tools +.. automodule:: diffpy.utils.transforms :members: :undoc-members: :show-inheritance: -diffpy.utils.resampler module -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +diffpy.utils.validators module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. automodule:: diffpy.utils.resampler +.. automodule:: diffpy.utils.validators :members: :undoc-members: :show-inheritance: +diffpy.utils.tools module +^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. automodule:: diffpy.utils.tools + :members: + :undoc-members: + :show-inheritance: diffpy.utils.user_config module -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. automodule:: diffpy.utils.user_config :members: @@ -47,9 +53,18 @@ diffpy.utils.user_config module :show-inheritance: diffpy.utils.diffraction_objects module -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. automodule:: diffpy.utils.diffraction_objects :members: :undoc-members: :show-inheritance: + +diffpy.utils.resampler module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. automodule:: diffpy.utils.resampler + :members: + :undoc-members: + :show-inheritance: + diff --git a/doc/source/api/diffpy.utils.wx.rst b/doc/source/api/diffpy.utils.wx.rst index 76c89035..60f60599 100644 --- a/doc/source/api/diffpy.utils.wx.rst +++ b/doc/source/api/diffpy.utils.wx.rst @@ -18,3 +18,4 @@ diffpy.utils.wx.gridutils module :members: :undoc-members: :show-inheritance: + From c65eace677d6e5aff1bf816d8bd69d5a5e7663f5 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Mon, 30 Dec 2024 22:27:32 -0500 Subject: [PATCH 2/6] refactor: rename isfloat to is_number, in a separate validators.py folder --- src/diffpy/utils/parsers/loaddata.py | 17 ++--------- src/diffpy/utils/validators.py | 45 ++++++++++++++++++++++++++++ tests/test_validators.py | 22 ++++++++++++++ 3 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 src/diffpy/utils/validators.py create mode 100644 tests/test_validators.py diff --git a/src/diffpy/utils/parsers/loaddata.py b/src/diffpy/utils/parsers/loaddata.py index 39c4b163..8369e516 100644 --- a/src/diffpy/utils/parsers/loaddata.py +++ b/src/diffpy/utils/parsers/loaddata.py @@ -17,6 +17,8 @@ import numpy +from diffpy.utils import validators + def loadData(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs): """Find and load data from a text file. @@ -139,7 +141,7 @@ def countcolumnsvalues(line): name = hpair[0] value = hpair[1] # check if data value should be stored as float - if isfloat(hpair[1]): + if validators.is_number(hpair[1]): value = float(hpair[1]) hdata.update({name: value}) # continue search for the start of datablock @@ -331,16 +333,3 @@ def _findDataBlocks(self): self.headers.append(header) self.datasets.append(data) return - - -# End of class TextDataLoader - - -def isfloat(s): - """True if s is convertible to float.""" - try: - float(s) - return True - except ValueError: - pass - return False diff --git a/src/diffpy/utils/validators.py b/src/diffpy/utils/validators.py new file mode 100644 index 00000000..0f8cd204 --- /dev/null +++ b/src/diffpy/utils/validators.py @@ -0,0 +1,45 @@ +def is_number(string): + """ + Check if the provided string can be converted to a float. + + Parameters + ---------- + string : str + The string to evaluate for numeric conversion. + + Returns + ------- + bool + The boolean whether `string` can be successfully converted to float. + + Examples + -------- + >>> is_number("3.14") + True + + >>> is_number("-1.23") + True + + >>> is_number("007") + True + + >>> is_number("five") + False + + >>> is_number("3.14.15") + False + + >>> is_number("NaN") + True + + >>> is_number("Infinity") + True + + >>> is_number("Inf") + True + """ + try: + float(string) + return True + except ValueError: + return False \ No newline at end of file diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 00000000..0d732e77 --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,22 @@ +from diffpy.utils.validators import is_number +import pytest + +@pytest.mark.parametrize("input,expected", [ + ("3.14", True), # Standard float + ("2", True), # Integer + ("-100", True), # Negative integer + ("-3.14", True), # Negative float + ("0", True), # Zero + ("4.5e-1", True), # Scientific notation + ("abc", False), # Non-numeric string + ("", False), # Empty string + ("3.14.15", False), # Multiple dots + ("2+3", False), # Arithmetic expression + ("NaN", True), # Not a Number (special float value) + ("Infinity", True), # Positive infinity + ("-Infinity", True), # Negative infinity + ("Inf", True), # Positive infinity + ("-Inf", True), # Negative infinity +]) +def test_is_number(input, expected): + assert is_number(input) == expected \ No newline at end of file From 23c32d6ec7a9c5e6431f15793a5eebaa5d21ec39 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Mon, 30 Dec 2024 22:27:38 -0500 Subject: [PATCH 3/6] chore: add news file --- news/is-float.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/is-float.rst diff --git a/news/is-float.rst b/news/is-float.rst new file mode 100644 index 00000000..918600e5 --- /dev/null +++ b/news/is-float.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* Rename the `isfloat` function to `is_number`, and move it to the `diffpy/utils/utilsvalidators.py` directory + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From 7d6dbc1cd1573200ee4766d9c9f2d84e707fcd25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:30:08 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit hooks --- doc/source/api/diffpy.utils.parsers.rst | 1 - doc/source/api/diffpy.utils.rst | 1 - doc/source/api/diffpy.utils.wx.rst | 1 - src/diffpy/utils/validators.py | 9 +++--- tests/test_validators.py | 43 ++++++++++++++----------- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/doc/source/api/diffpy.utils.parsers.rst b/doc/source/api/diffpy.utils.parsers.rst index b08a1840..29ec4782 100644 --- a/doc/source/api/diffpy.utils.parsers.rst +++ b/doc/source/api/diffpy.utils.parsers.rst @@ -34,4 +34,3 @@ diffpy.utils.parsers.serialization module :members: :undoc-members: :show-inheritance: - diff --git a/doc/source/api/diffpy.utils.rst b/doc/source/api/diffpy.utils.rst index 128d914d..e4de1d4f 100644 --- a/doc/source/api/diffpy.utils.rst +++ b/doc/source/api/diffpy.utils.rst @@ -67,4 +67,3 @@ diffpy.utils.resampler module :members: :undoc-members: :show-inheritance: - diff --git a/doc/source/api/diffpy.utils.wx.rst b/doc/source/api/diffpy.utils.wx.rst index 60f60599..76c89035 100644 --- a/doc/source/api/diffpy.utils.wx.rst +++ b/doc/source/api/diffpy.utils.wx.rst @@ -18,4 +18,3 @@ diffpy.utils.wx.gridutils module :members: :undoc-members: :show-inheritance: - diff --git a/src/diffpy/utils/validators.py b/src/diffpy/utils/validators.py index 0f8cd204..dba25b70 100644 --- a/src/diffpy/utils/validators.py +++ b/src/diffpy/utils/validators.py @@ -1,6 +1,5 @@ def is_number(string): - """ - Check if the provided string can be converted to a float. + """Check if the provided string can be converted to a float. Parameters ---------- @@ -31,10 +30,10 @@ def is_number(string): >>> is_number("NaN") True - + >>> is_number("Infinity") True - + >>> is_number("Inf") True """ @@ -42,4 +41,4 @@ def is_number(string): float(string) return True except ValueError: - return False \ No newline at end of file + return False diff --git a/tests/test_validators.py b/tests/test_validators.py index 0d732e77..e340d065 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -1,22 +1,27 @@ -from diffpy.utils.validators import is_number import pytest -@pytest.mark.parametrize("input,expected", [ - ("3.14", True), # Standard float - ("2", True), # Integer - ("-100", True), # Negative integer - ("-3.14", True), # Negative float - ("0", True), # Zero - ("4.5e-1", True), # Scientific notation - ("abc", False), # Non-numeric string - ("", False), # Empty string - ("3.14.15", False), # Multiple dots - ("2+3", False), # Arithmetic expression - ("NaN", True), # Not a Number (special float value) - ("Infinity", True), # Positive infinity - ("-Infinity", True), # Negative infinity - ("Inf", True), # Positive infinity - ("-Inf", True), # Negative infinity -]) +from diffpy.utils.validators import is_number + + +@pytest.mark.parametrize( + "input,expected", + [ + ("3.14", True), # Standard float + ("2", True), # Integer + ("-100", True), # Negative integer + ("-3.14", True), # Negative float + ("0", True), # Zero + ("4.5e-1", True), # Scientific notation + ("abc", False), # Non-numeric string + ("", False), # Empty string + ("3.14.15", False), # Multiple dots + ("2+3", False), # Arithmetic expression + ("NaN", True), # Not a Number (special float value) + ("Infinity", True), # Positive infinity + ("-Infinity", True), # Negative infinity + ("Inf", True), # Positive infinity + ("-Inf", True), # Negative infinity + ], +) def test_is_number(input, expected): - assert is_number(input) == expected \ No newline at end of file + assert is_number(input) == expected From 84dcb4e6256169202f9bb439079f502976147d6b Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Mon, 30 Dec 2024 22:46:54 -0500 Subject: [PATCH 5/6] docs: improve description on how int is also a float --- src/diffpy/utils/validators.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/diffpy/utils/validators.py b/src/diffpy/utils/validators.py index dba25b70..0f281073 100644 --- a/src/diffpy/utils/validators.py +++ b/src/diffpy/utils/validators.py @@ -1,5 +1,8 @@ def is_number(string): """Check if the provided string can be converted to a float. + + Since integers can be converted to floats, this function will return True for integers as well. + Hence, we can use this function to check if a string is a number. Parameters ---------- From f8b1c41be6fe43cbfcf2d58b50d2c892d6662460 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:47:03 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit hooks --- src/diffpy/utils/validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffpy/utils/validators.py b/src/diffpy/utils/validators.py index 0f281073..91a461bf 100644 --- a/src/diffpy/utils/validators.py +++ b/src/diffpy/utils/validators.py @@ -1,6 +1,6 @@ def is_number(string): """Check if the provided string can be converted to a float. - + Since integers can be converted to floats, this function will return True for integers as well. Hence, we can use this function to check if a string is a number.