Skip to content

Commit 52f1c79

Browse files
authored
Merge pull request #301 from bobleesj/isfloat
refactor: separate `isfloat` into a new file within `validators.py` and rename to `is_number`
2 parents 9ce1769 + f8b1c41 commit 52f1c79

File tree

6 files changed

+131
-31
lines changed

6 files changed

+131
-31
lines changed

Diff for: doc/source/api/diffpy.utils.parsers.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ diffpy.utils.parsers package
1111
Submodules
1212
----------
1313

14-
diffpy.utils.parsers.serialization module
15-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16-
17-
.. automodule:: diffpy.utils.parsers.serialization
18-
:members:
19-
:undoc-members:
20-
:show-inheritance:
21-
2214
diffpy.utils.parsers.loaddata module
2315
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2416

@@ -34,3 +26,11 @@ diffpy.utils.parsers.custom_exceptions module
3426
:members:
3527
:undoc-members:
3628
:show-inheritance:
29+
30+
diffpy.utils.parsers.serialization module
31+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
.. automodule:: diffpy.utils.parsers.serialization
34+
:members:
35+
:undoc-members:
36+
:show-inheritance:

Diff for: doc/source/api/diffpy.utils.rst

+23-9
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,54 @@ Subpackages
1616

1717
diffpy.utils.parsers
1818
diffpy.utils.wx
19-
diffpy.utils.scattering_objects
2019

2120
Submodules
2221
----------
2322

24-
diffpy.utils.tools module
25-
^^^^^^^^^^^^^^^^^^^^^^^^^
23+
diffpy.utils.transforms module
24+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2625

27-
.. automodule:: diffpy.utils.tools
26+
.. automodule:: diffpy.utils.transforms
2827
:members:
2928
:undoc-members:
3029
:show-inheritance:
3130

32-
diffpy.utils.resampler module
33-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
diffpy.utils.validators module
32+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3433

35-
.. automodule:: diffpy.utils.resampler
34+
.. automodule:: diffpy.utils.validators
3635
:members:
3736
:undoc-members:
3837
:show-inheritance:
3938

39+
diffpy.utils.tools module
40+
^^^^^^^^^^^^^^^^^^^^^^^^^
41+
42+
.. automodule:: diffpy.utils.tools
43+
:members:
44+
:undoc-members:
45+
:show-inheritance:
4046

4147
diffpy.utils.user_config module
42-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4349

4450
.. automodule:: diffpy.utils.user_config
4551
:members:
4652
:undoc-members:
4753
:show-inheritance:
4854

4955
diffpy.utils.diffraction_objects module
50-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5157

5258
.. automodule:: diffpy.utils.diffraction_objects
5359
:members:
5460
:undoc-members:
5561
:show-inheritance:
62+
63+
diffpy.utils.resampler module
64+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
66+
.. automodule:: diffpy.utils.resampler
67+
:members:
68+
:undoc-members:
69+
:show-inheritance:

Diff for: news/is-float.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* Rename the `isfloat` function to `is_number`, and move it to the `diffpy/utils/utilsvalidators.py` directory
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

Diff for: src/diffpy/utils/parsers/loaddata.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import numpy
1919

20+
from diffpy.utils import validators
21+
2022

2123
def loadData(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs):
2224
"""Find and load data from a text file.
@@ -139,7 +141,7 @@ def countcolumnsvalues(line):
139141
name = hpair[0]
140142
value = hpair[1]
141143
# check if data value should be stored as float
142-
if isfloat(hpair[1]):
144+
if validators.is_number(hpair[1]):
143145
value = float(hpair[1])
144146
hdata.update({name: value})
145147
# continue search for the start of datablock
@@ -331,16 +333,3 @@ def _findDataBlocks(self):
331333
self.headers.append(header)
332334
self.datasets.append(data)
333335
return
334-
335-
336-
# End of class TextDataLoader
337-
338-
339-
def isfloat(s):
340-
"""True if s is convertible to float."""
341-
try:
342-
float(s)
343-
return True
344-
except ValueError:
345-
pass
346-
return False

Diff for: src/diffpy/utils/validators.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def is_number(string):
2+
"""Check if the provided string can be converted to a float.
3+
4+
Since integers can be converted to floats, this function will return True for integers as well.
5+
Hence, we can use this function to check if a string is a number.
6+
7+
Parameters
8+
----------
9+
string : str
10+
The string to evaluate for numeric conversion.
11+
12+
Returns
13+
-------
14+
bool
15+
The boolean whether `string` can be successfully converted to float.
16+
17+
Examples
18+
--------
19+
>>> is_number("3.14")
20+
True
21+
22+
>>> is_number("-1.23")
23+
True
24+
25+
>>> is_number("007")
26+
True
27+
28+
>>> is_number("five")
29+
False
30+
31+
>>> is_number("3.14.15")
32+
False
33+
34+
>>> is_number("NaN")
35+
True
36+
37+
>>> is_number("Infinity")
38+
True
39+
40+
>>> is_number("Inf")
41+
True
42+
"""
43+
try:
44+
float(string)
45+
return True
46+
except ValueError:
47+
return False

Diff for: tests/test_validators.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
3+
from diffpy.utils.validators import is_number
4+
5+
6+
@pytest.mark.parametrize(
7+
"input,expected",
8+
[
9+
("3.14", True), # Standard float
10+
("2", True), # Integer
11+
("-100", True), # Negative integer
12+
("-3.14", True), # Negative float
13+
("0", True), # Zero
14+
("4.5e-1", True), # Scientific notation
15+
("abc", False), # Non-numeric string
16+
("", False), # Empty string
17+
("3.14.15", False), # Multiple dots
18+
("2+3", False), # Arithmetic expression
19+
("NaN", True), # Not a Number (special float value)
20+
("Infinity", True), # Positive infinity
21+
("-Infinity", True), # Negative infinity
22+
("Inf", True), # Positive infinity
23+
("-Inf", True), # Negative infinity
24+
],
25+
)
26+
def test_is_number(input, expected):
27+
assert is_number(input) == expected

0 commit comments

Comments
 (0)