-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathvalidators.py
More file actions
48 lines (37 loc) · 901 Bytes
/
validators.py
File metadata and controls
48 lines (37 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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