test_unpack.py

来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 106 行

PY
106
字号
from test_support import *print_test("Sequence Unpacking (test_unpack.py)", 2)t = (1, 2, 3)l = [4, 5, 6]class Seq:    def __getitem__(self, i):        if i >= 0 and i < 3: return i        raise IndexErrora = -1b = -1c = -1# unpack tupleprint_test("tuple", 3)a, b, c = tassert a == 1 and b == 2 and c == 3print_test("list", 3)a, b, c = lassert a == 4 and b == 5 and c == 6print_test("inline tuple")a, b, c = 7, 8, 9assert a == 7 and b == 8 and c == 9print_test("string")a, b, c = 'one'assert a == 'o' and b == 'n' and c == 'e'print_test("generic sequence")a, b, c = Seq()assert a == 0 and b == 1 and c == 2# now for some failuresprint_test("failures")print_test("non-sequence", 4)try:    a, b, c = 7    raise TestFailedexcept TypeError:    passprint_test("wrong size tuple")try:    a, b = t    raise TestFailedexcept ValueError:    pass    print_test("wrong size list")try:    a, b = l    raise TestFailedexcept ValueError:    passprint_test("sequence too short")try:    a, b, c, d = Seq()    raise TestFailedexcept ValueError:    passprint_test("sequence too long")try:    a, b = Seq()    raise TestFailedexcept ValueError:    pass# unpacking a sequence where the test for too long raises a different# kind of errorBozoError = 'BozoError'class BadSeq:    def __getitem__(self, i):        if i >= 0 and i < 3:            return i        elif i == 3:            raise BozoError        else:            raise IndexErrorprint_test("sequence too long, wrong error")try:    a, b, c, d, e = BadSeq()    raise TestFailedexcept BozoError:    passprint_test("sequence too short, wrong error")try:    a, b, c = BadSeq()    raise TestFailedexcept BozoError:    pass

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?