File: utiltexttest.py

package info (click to toggle)
python-beartype 0.22.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,504 kB
  • sloc: python: 85,502; sh: 328; makefile: 30; javascript: 18
file content (46 lines) | stat: -rw-r--r-- 1,673 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
# --------------------( LICENSE                            )--------------------
# Copyright (c) 2014-2025 Beartype authors.
# See "LICENSE" for further details.

'''
**Beartype string testing utilities** (i.e., callables testing whether passed
strings satisfy various conditions).

This private submodule is *not* intended for importation by downstream callers.
'''

# ....................{ IMPORTS                            }....................

# ....................{ TESTERS                            }....................
def is_str_float_or_int(text: str) -> bool:
    '''
    ``True`` only if the passed string is a valid machine-readable
    representation of either an integer or finite floating-point number.

    Caveats
    ----------
    This tester intentionally returns ``False`` for non-standard floating-point
    pseudo-numbers that have no finite value, including:

    * Not-a-numbers (i.e., ``float('NaN')`` values).
    * Negative infinity (i.e., ``float('-inf')`` values).
    * Positive infinity (i.e., ``float('inf')`` values).

    Parameters
    ----------
    text : str
        String to be inspected.

    Returns
    ----------
    bool
        ``True`` only if this string is a valid machine-readable representation
        of either an integer or finite floating-point number.
    '''
    assert isinstance(text, str), f'{repr(text)} not string.'

    # Return true only if this text represents a finite number. See also:
    #     s.lstrip('-').replace('.','',1).replace('e-','',1).replace('e','',1).isdigit()
    return text.lstrip(
        '-').replace('.','',1).replace('e-','',1).replace('e','',1).isdigit()