MCPcopy Create free account
hub / github.com/astanin/python-tabulate / _isnumber

Function _isnumber

tabulate/__init__.py:903–952  ·  view source on GitHub ↗

Detects if something *could* be considered a numeric value, vs. just a string. This promotes types convertible to both int and float to be considered a float. Note that, iff *all* values appear to be some form of numeric value such as eg. "1e2", they would be considered numbers! T

(string)

Source from the content-addressed store, hash-verified

901
902
903def _isnumber(string):
904 """Detects if something *could* be considered a numeric value, vs. just a string.
905
906 This promotes types convertible to both int and float to be considered
907 a float. Note that, iff *all* values appear to be some form of numeric
908 value such as eg. "1e2", they would be considered numbers!
909
910 The exception is things that appear to be numbers but overflow to
911 +/-inf, eg. "1e23456"; we'll have to exclude them explicitly.
912
913 >>> _isnumber(123)
914 True
915 >>> _isnumber(123.45)
916 True
917 >>> _isnumber("123.45")
918 True
919 >>> _isnumber("123")
920 True
921 >>> _isnumber("spam")
922 False
923 >>> _isnumber("123e45")
924 True
925 >>> _isnumber("123e45678") # evaluates equal to 'inf', but ... isn't
926 False
927 >>> _isnumber("inf")
928 True
929 >>> from fractions import Fraction
930 >>> _isnumber(Fraction(1,3))
931 True
932
933 """
934 return (
935 # fast path
936 type(string) in (float, int)
937 # covers 'NaN', +/- 'inf', and eg. '1e2', as well as any type
938 # convertible to int/float.
939 or (
940 _isconvertible(float, string)
941 and (
942 # some other type convertible to float
943 not isinstance(string, (str, bytes))
944 # or, a numeric string eg. "1e1...", "NaN", ..., but isn't
945 # just an over/underflow
946 or (
947 not (math.isinf(float(string)) or math.isnan(float(string)))
948 or string.lower() in ["inf", "-inf", "nan"]
949 )
950 )
951 )
952 )
953
954
955def _isint(string, inttype=int):

Callers 3

_typeFunction · 0.85
_afterpointFunction · 0.85
_wrap_text_to_colwidthsFunction · 0.85

Calls 1

_isconvertibleFunction · 0.85

Tested by

no test coverage detected