diff --git a/doc/source/api/diffpy.utils.parsers.rst b/doc/source/api/diffpy.utils.parsers.rst index 3456f24e..29ec4782 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,11 @@ 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..e4de1d4f 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,17 @@ 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/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:** + +* 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..91a461bf --- /dev/null +++ b/src/diffpy/utils/validators.py @@ -0,0 +1,47 @@ +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 + ---------- + 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 diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 00000000..e340d065 --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,27 @@ +import pytest + +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