Skip to content

Commit c65eace

Browse files
committed
refactor: rename isfloat to is_number, in a separate validators.py folder
1 parent 77c5957 commit c65eace

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

src/diffpy/utils/parsers/loaddata.py

Lines changed: 3 additions & 14 deletions
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

src/diffpy/utils/validators.py

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

tests/test_validators.py

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

0 commit comments

Comments
 (0)