Skip to content

refactor: separate isfloat into a new file within validators.py and rename to is_number #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 31, 2024
Merged
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
16 changes: 8 additions & 8 deletions doc/source/api/diffpy.utils.parsers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -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:
32 changes: 23 additions & 9 deletions doc/source/api/diffpy.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,54 @@ 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:
:undoc-members:
: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:
23 changes: 23 additions & 0 deletions news/is-float.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* Rename the `isfloat` function to `is_number`, and move it to the `diffpy/utils/utilsvalidators.py` directory

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
17 changes: 3 additions & 14 deletions src/diffpy/utils/parsers/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import numpy

from diffpy.utils import validators
Copy link
Contributor Author

@bobleesj bobleesj Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import is_number from validators



def loadData(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs):
"""Find and load data from a text file.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
47 changes: 47 additions & 0 deletions src/diffpy/utils/validators.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -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
Loading