提供了以下代碼
from typing import Union, Tuple
import unittest
def split_value_and_unit(inp_value: Union[str, int, float]) -> Tuple[float, str]:
unit = ''
value: float
if (isinstance(inp_value, int) or isinstance(inp_value, float)):
value = float(inp_value)
else:
# get rid of = sign at the beginning of string
str_value = inp_value
if (inp_value.startswith('=')):
str_value = inp_value[1:]
if ('*' in inp_value):
sep_index = str_value.index('*')
try:
value = float(str_value[:sep_index])
except ValueError:
raise Exception(f'Expected a string containing a float number and a unit. "{str(inp_value)}" does not fulfil this requirement')
unit = str_value[sep_index 1:]
else:
try:
value = float(str_value)
except ValueError:
raise Exception(f'Expected a string containing a float number and a unit. "{str(inp_value)}" does not fulfil this requirement')
return (value, unit)
class TestX(unittest.TestCase):
def test_split_value_and_unit(self):
# self.assertRaisesRegex(Exception, r'Expected a string containing a float number and a unit. "_0" does not fulfil this requirement', lambda: split_value_and_unit('_0'))
self.assertRaisesRegex(Exception, 'Expected a string containing a float number and a unit. "_1 e16" does not fulfil this requirement', lambda: split_value_and_unit('_1 e16'))
我本來希望單元測驗通過。但是運行會coverage run -m unittest -v test.x回傳以下輸出:
test_split_value_and_unit (test.x.TestX) ... FAIL
======================================================================
FAIL: test_split_value_and_unit (test.x.TestX)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\klosemic\Documents\SVN_wc\trunk\Tools\ng_scripts\orchid_ng\test\x.py", line 24, in split_value_and_unit
value = float(str_value)
ValueError: could not convert string to float: '_1 e16'
During handling of the above exception, another exception occurred:
Exception: Expected a string containing a float number and a unit. "_1 e16" does not fulfil this requirement
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\klosemic\Documents\SVN_wc\trunk\Tools\ng_scripts\orchid_ng\test\x.py", line 33, in test_split_value_and_unit
self.assertRaisesRegex(Exception, 'Expected a string containing a float number and a unit. "_1 e16" does not fulfil this requirement', lambda: split_value_and_unit('_1 e16'))
AssertionError: "Expected a string containing a float number and a unit. "_1 e16" does not fulfil this requirement" does not match "Expected a string containing a float number and a unit. "_1 e16" does not fulfil this requirement"
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
如果我交換測驗例外的兩行(所以如果我通過“_0”而不是“_1e 16”)測驗通過。
所以我的問題是,為什么“_1e 16”與測驗用例的“_0”不同?尤其是當我在函式中明確處理 ValueError 時(如果這不是很好的風格不是這里的重點)。
提前致謝
uj5u.com熱心網友回復:
self.assertRaisesRegex將正則運算式作為第二個引數。由于您的正則運算式包含通配符 ( .) 和量詞 ( ),因此您的字串不再匹配。
有關更多詳細資訊,請參閱https://regex101.com/r/GPwpTV/1與https://regex101.com/r/Z9O7tP/1。
您正在尋找的正則運算式需要轉義這些字符如下:
r'Expected a string containing a float number and a unit\. "_1\ e16" does not fulfil this requirement'
沒有它,量詞 將匹配1一次,然后沒有任何匹配的文字 。
完整代碼如下:
from typing import Union, Tuple
import unittest
def split_value_and_unit(inp_value: Union[str, int, float]) -> Tuple[float, str]:
unit = ''
value: float
if (isinstance(inp_value, int) or isinstance(inp_value, float)):
value = float(inp_value)
else:
# get rid of = sign at the beginning of string
str_value = inp_value
if (inp_value.startswith('=')):
str_value = inp_value[1:]
if ('*' in inp_value):
sep_index = str_value.index('*')
try:
value = float(str_value[:sep_index])
except ValueError:
raise Exception(f'Expected a string containing a float number and a unit. "{str(inp_value)}" does not fulfil this requirement')
unit = str_value[sep_index 1:]
else:
try:
value = float(str_value)
except ValueError:
raise Exception(f'Expected a string containing a float number and a unit. "{str(inp_value)}" does not fulfil this requirement')
return (value, unit)
class TestX(unittest.TestCase):
def test_split_value_and_unit(self):
# self.assertRaisesRegex(Exception, r'Expected a string containing a float number and a unit. "_0" does not fulfil this requirement', lambda: split_value_and_unit('_0'))
self.assertRaisesRegex(Exception, r'Expected a string containing a float number and a unit\. "_1\ e16" does not fulfil this requirement', lambda: split_value_and_unit('_1 e16'))
或者import re,re.escape('Expected a string containing a float number and a unit. "_1 e16" does not fulfil this requirement')如果您不想擔心總是轉義特殊字符,則可以將其用作模式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/317346.html
上一篇:如何在Xamarin后面的代碼中系結StackLayout資料
下一篇:使用正則運算式提取子字串
