Python源码示例:unicodedata.numeric()
示例1
def test_ipy2_gh357(self):
"""https://github.com/IronLanguages/ironpython2/issues/357"""
import unicodedata
if is_cli:
self.assertEqual(unicodedata.name(u'\u4e2d'), '<CJK IDEOGRAPH, FIRST>..<CJK IDEOGRAPH, LAST>')
else:
self.assertEqual(unicodedata.name(u'\u4e2d'), 'CJK UNIFIED IDEOGRAPH-4E2D')
self.assertRaises(ValueError, unicodedata.decimal, u'\u4e2d')
self.assertEqual(unicodedata.decimal(u'\u4e2d', 0), 0)
self.assertRaises(ValueError, unicodedata.digit, u'\u4e2d')
self.assertEqual(unicodedata.digit(u'\u4e2d', 0), 0)
self.assertRaises(ValueError, unicodedata.numeric, u'\u4e2d')
self.assertEqual(unicodedata.numeric(u'\u4e2d', 0), 0)
self.assertEqual(unicodedata.category(u'\u4e2d'), 'Lo')
self.assertEqual(unicodedata.bidirectional(u'\u4e2d'), 'L')
self.assertEqual(unicodedata.combining(u'\u4e2d'), 0)
self.assertEqual(unicodedata.east_asian_width(u'\u4e2d'), 'W')
self.assertEqual(unicodedata.mirrored(u'\u4e2d'), 0)
self.assertEqual(unicodedata.decomposition(u'\u4e2d'), '')
示例2
def convert_floats(data, integers=None):
"""
Convert all numeric values in dictionary to float type.
"""
integers = integers or []
delete_keys = []
for key, value in data.iteritems():
try:
if isinstance(value, datetime.datetime):
continue
if is_number(value):
if key in integers:
data[key] = int(value)
else:
data[key] = float(value)
if math.isnan(data[key]):
delete_keys.append(key)
except:
pass
for key in delete_keys:
del data[key]
return data
示例3
def is_number(s):
"""
Check string for being a numeric value.
http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
"""
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例4
def __IsNumber(self, input_data):
result = False
try:
float(input_data)
result = True
except ValueError:
pass
if result:
temp = float(input_data)
if np.isnan(temp):
return False
if np.isinf(temp):
return False
if not result:
try:
import unicodedata
unicodedata.numeric(input_data)
result = True
except (TypeError, ValueError):
pass
return result
示例5
def is_number(s):
"""Determines if the input is numeric
Args:
s: The value to check.
Returns:
bool: ``True`` if the input is numeric, ``False`` otherwise.
"""
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#----------------------------------------------------------------------
示例6
def is_number(s):
result = False
try:
float(s)
result = True
except ValueError:
pass
if not result:
try:
import unicodedata
unicodedata.numeric(s)
result = True
except (TypeError, ValueError):
pass
return result
示例7
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#----------------------------------------------------------------------
示例8
def test_ipy2_gh357(self):
"""https://github.com/IronLanguages/ironpython2/issues/357"""
import unicodedata
if is_cli:
self.assertEqual(unicodedata.name(u'\u4e2d'), '<CJK IDEOGRAPH, FIRST>..<CJK IDEOGRAPH, LAST>')
else:
self.assertEqual(unicodedata.name(u'\u4e2d'), 'CJK UNIFIED IDEOGRAPH-4E2D')
self.assertRaises(ValueError, unicodedata.decimal, u'\u4e2d')
self.assertEqual(unicodedata.decimal(u'\u4e2d', 0), 0)
self.assertRaises(ValueError, unicodedata.digit, u'\u4e2d')
self.assertEqual(unicodedata.digit(u'\u4e2d', 0), 0)
self.assertRaises(ValueError, unicodedata.numeric, u'\u4e2d')
self.assertEqual(unicodedata.numeric(u'\u4e2d', 0), 0)
self.assertEqual(unicodedata.category(u'\u4e2d'), 'Lo')
self.assertEqual(unicodedata.bidirectional(u'\u4e2d'), 'L')
self.assertEqual(unicodedata.combining(u'\u4e2d'), 0)
self.assertEqual(unicodedata.east_asian_width(u'\u4e2d'), 'W')
self.assertEqual(unicodedata.mirrored(u'\u4e2d'), 0)
self.assertEqual(unicodedata.decomposition(u'\u4e2d'), '')
示例9
def is_number(s):
"""
Helper function to detect numbers
Args:
s: a string
Returns:
bool: True if s is a number
"""
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例10
def is_number(string):
"""Also accepts '.' in the string. Function 'isnumeric()' doesn't"""
try:
float(string)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(string)
return True
except (TypeError, ValueError):
pass
return False
示例11
def isNumber(s):
"""
Determines if a unicode string (that may include commas) is a number.
:param s: Any unicode string.
:return: True if s represents a number, False otherwise.
"""
s = s.replace(',', '')
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError) as e:
pass
return False
示例12
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#主要程序部分
示例13
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#读取配置文件信息
示例14
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#主要程序部分
示例15
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
# Take image function
示例16
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
########################################################################################################################
示例17
def fraction_to_float(fraction: str) -> float:
"""Convert string representation of a fraction to float.
Also supports unicode characters.
Args:
fraction (str): String representation of fraction, ie. "3/4", "1 1/2", etc.
Returns:
float: Converted fraction
"""
# For fractions with weird divider character (ie. "1⁄2")
fraction = fraction.replace("⁄", "/")
try:
# Convert unicode fractions (ie. "½")
fraction_out = unicodedata.numeric(fraction)
except TypeError:
try:
# Convert normal fraction (ie. "1/2")
fraction_out = float(sum(Fraction(s) for s in fraction.split()))
except ValueError:
# Convert combined fraction with unicode (ie. "1 ½")
fraction_split = fraction.split()
fraction_out = float(fraction_split[0]) + unicodedata.numeric(
fraction_split[1]
)
return fraction_out
示例18
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例19
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
########################################################################################################################
示例20
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例21
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例22
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例23
def safe_submit_metric(self, name, value, tags=None):
tags = [] if tags is None else tags
try:
self.gauge(name, float(value), tags=tags)
return
except ValueError:
self.log.debug("metric name %s cannot be converted to a float: %s", name, value)
try:
self.gauge(name, unicodedata.numeric(value), tags=tags)
return
except (TypeError, ValueError):
self.log.debug("metric name %s cannot be converted to a float even using unicode tools: %s", name, value)
示例24
def is_digit(word):
try:
float(word)
return True
except ValueError:
pass
try:
unicodedata.numeric(word)
return True
except (TypeError, ValueError):
pass
result = re.compile(r'^[-+]?[0-9]+,[0-9]+$').match(word)
if result:
return True
return False
示例25
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例26
def Is_Number(s):
num_flag = False
try:
float(s)
num_flag = True
except ValueError:
pass
if not num_flag:
try:
unicodedata.numeric(s)
num_flag = True
except (TypeError, ValueError):
pass
return num_flag
示例27
def To_Number(cell):
if cell.lower() == 'nan' or cell.lower() == 'inf':
return 0.0
try:
v = float(cell)
return v
except ValueError:
pass
try:
v = unicodedata.numeric(cell)
return v
except (TypeError, ValueError):
pass
return 0.0
示例28
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
示例29
def is_number(string: str) -> bool:
"""See if a given string is a number (int or float)"""
try:
float(string)
return True
except ValueError:
pass
try:
unicodedata.numeric(string)
return True
except (TypeError, ValueError):
pass
return False
示例30
def safe_submit_metric(self, name, value, tags=None):
tags = [] if tags is None else tags
try:
self.gauge(name, float(value), tags=tags)
return
except ValueError:
self.log.debug("metric name %s cannot be converted to a float: %s", name, value)
pass
try:
self.gauge(name, unicodedata.numeric(value), tags=tags)
return
except (TypeError, ValueError):
self.log.debug("metric name %s cannot be converted to a float even using unicode tools: %s", name, value)
pass