Python: 如何强制将一个值解释为单个字符串元组,而不是字符集合?

huangapple go评论51阅读模式
英文:

Python: How to I force interpretation of a value as a single tuple of one string, not a collection of characters?

问题

我正在使用Python 3.10.8。

我有一个函数,可以将使用正则表达式分隔的字符串拆分成任意长度的元组。我想要计算从我的函数返回的子字符串数量。但是当源字符串没有分隔符时,我的函数会正确地返回一个包含单个字符串的元组,内置的len()函数会返回字符串的长度。我如何知道/强制返回值是一个单个字符串,而不是一组字符?
这个测试函数不像预期的那样工作:

def test_da_tuple(subject_string, expected_length):
    da_tuple = MangleSplitter.tuple_of(subject_string)
    pprint.pprint(da_tuple)
    tuple_len = len(da_tuple)
    assert tuple_len == expected_length, ("\"%s\" split into %d not %d" % (subject_string, tuple_len, expected_length))

还有一些示例:

MANGLED_STR_00 = "Jack L. Chalker - Demons of the Dancing GodsUC - #2DG"
CRAZYNESS = "A - B - C - D - F - F - G - H - I"
MANGLED_STR_07 = "Book Over"

我希望我的test_da_tuple()函数可以验证MANGLED_STR_00为3,CRAZYNESS为9,MANGLED_STR_07为1。但是我得到一个断言错误,MANGLED_STR_07分割为9而不是1。

英文:

I'm using Python Python 3.10.8

I have a function that splits regex delimited strings into a tuple of arbitrary length. I want to count the number of sub-strings returned from my function. But when the source string does not have the delimiter, and my function correctly returns a tuple with a single string, the built-in len() returns the length of the string. How can I know/force that the return value is a single string, and not a collection of characters?
This test function does not work as desired:

def test_da_tuple(subject_string, expected_length):
    da_tuple = MangleSplitter.tuple_of(subject_string)
    pprint.pprint(da_tuple)
    tuple_len = len(da_tuple)
    assert tuple_len == expected_length, ("\"%s\" split into %d not %d" % (subject_string, tuple_len, expected_length))

And some samples

MANGLED_STR_00 = "Jack L. Chalker - Demons of the Dancing GodsUC - #2DG"
CRAZYNESS = "A - B - C - D - F - F - G - H - I"
MANGLED_STR_07 = "Book Over"

I want my test_da_tuple() to verify 3 for MANGLED_STR_00, 9 for CRAZYNESS, and 1 for MANGLED_STR_07. Instead I get an assertion error that MANGLED_STR_07 split into 9 not 1.

答案1

得分: 1

以下是翻译好的部分:

... my function correctly returns a tuple with a single string, the built-in len() returns the length of the string.

不,你的函数没有正确遵循它的合同,没有遵守它所作出的承诺。

如果你的函数返回 t = ('foo', 'bar'),那么 len(t) 将报告 2,很好。

如果你的函数返回一个单字符串 t = 'baz',那完全不同,长度将报告为 3
你实际想要的是一个 1-元组:t = ('baz',)


使用 print() 调试,或者 assert isinstance( ... , tuple)
来验证它返回的是一个 tuple 而不是一个 str

编写一个 Red 单元测试,突出显示你抱怨的当前问题,
然后添加一个错误修复,使测试变为 Green。


编辑

是否有区别

嗯,你选择如何返回这个值肯定会有差异。
这里有三个元组。其中一些符合函数的合同。

>>> tuple(('foo', 'bar'))
('foo', 'bar')
>>> 
>>> tuple('abc')
('a', 'b', 'c')
>>> 
>>> tuple(('def',))
('def',)
>>> 
英文:

> ... my function correctly returns a tuple with a single string, the built-in len() returns the length of the string.

No, your function is not correctly adhering to its contract,
to the promises it makes.

If your function returns t = ('foo', 'bar') then len(t) will report 2, good.

If your function returns a single string t = 'baz',
that is completely different and the length is reported as 3.
What you wanted instead was a 1-tuple: t = ('baz',)


Use print() debugging, or assert instanceof( ... , tuple),
to verify it is returning a tuple rather than a str.

Write a Red unit test which highlights the current lossage
you're complaining of, and then add a bug fix so the tests turns Green.


EDIT

> is there a difference

Well, it certainly makes a difference how you choose to return the value.
Here are three tuples. Some of them fit the function's contract.

>>> tuple(('foo', 'bar'))
('foo', 'bar')
>>> 
>>> tuple('abc')
('a', 'b', 'c')
>>> 
>>> tuple(('def',))
('def',)
>>> 

huangapple
  • 本文由 发表于 2023年2月24日 08:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定