2014-04-13 03:17:04 -04:00
|
|
|
try:
|
2022-08-18 02:57:45 -04:00
|
|
|
from collections import namedtuple
|
2014-04-13 03:17:04 -04:00
|
|
|
except ImportError:
|
2017-02-15 10:11:16 -05:00
|
|
|
print("SKIP")
|
2017-06-10 13:03:01 -04:00
|
|
|
raise SystemExit
|
2014-02-27 15:22:04 -05:00
|
|
|
|
2014-12-20 10:37:40 -05:00
|
|
|
T = namedtuple("Tup", ["foo", "bar"])
|
2014-02-27 15:22:04 -05:00
|
|
|
# CPython prints fully qualified name, what we don't bother to do so far
|
|
|
|
#print(T)
|
2014-12-20 10:38:40 -05:00
|
|
|
for t in T(1, 2), T(bar=1, foo=2):
|
|
|
|
print(t)
|
|
|
|
print(t[0], t[1])
|
|
|
|
print(t.foo, t.bar)
|
2014-02-27 15:22:04 -05:00
|
|
|
|
2014-12-20 10:38:40 -05:00
|
|
|
print(len(t))
|
|
|
|
print(bool(t))
|
|
|
|
print(t + t)
|
|
|
|
print(t * 3)
|
2014-02-27 15:49:47 -05:00
|
|
|
|
2014-12-20 10:38:40 -05:00
|
|
|
print([f for f in t])
|
2014-05-10 13:15:49 -04:00
|
|
|
|
2014-12-20 10:38:40 -05:00
|
|
|
print(isinstance(t, tuple))
|
2014-02-27 15:22:04 -05:00
|
|
|
|
2020-02-10 06:25:52 -05:00
|
|
|
# Check tuple can compare equal to namedtuple with same elements
|
|
|
|
print(t == (t[0], t[1]), (t[0], t[1]) == t)
|
|
|
|
|
2017-06-29 03:49:10 -04:00
|
|
|
# Create using positional and keyword args
|
|
|
|
print(T(3, bar=4))
|
|
|
|
|
2014-02-27 15:22:04 -05:00
|
|
|
try:
|
|
|
|
t[0] = 200
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
try:
|
|
|
|
t.bar = 200
|
|
|
|
except AttributeError:
|
2014-12-20 10:37:40 -05:00
|
|
|
print("AttributeError")
|
2014-02-27 15:22:04 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
t = T(1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
t = T(1, 2, 3)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2014-12-20 10:38:40 -05:00
|
|
|
try:
|
|
|
|
t = T(foo=1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
t = T(1, foo=1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2015-04-04 19:03:43 -04:00
|
|
|
# enough args, but kw is wrong
|
|
|
|
try:
|
|
|
|
t = T(1, baz=3)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
# bad argument for member spec
|
|
|
|
try:
|
|
|
|
namedtuple('T', 1)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2014-12-20 10:37:40 -05:00
|
|
|
# Try single string
|
2015-04-04 19:03:43 -04:00
|
|
|
T3 = namedtuple("TupComma", "foo bar")
|
|
|
|
t = T3(1, 2)
|
|
|
|
print(t.foo, t.bar)
|
2014-12-20 10:37:40 -05:00
|
|
|
|
2016-05-22 13:28:04 -04:00
|
|
|
# Try tuple
|
|
|
|
T4 = namedtuple("TupTuple", ("foo", "bar"))
|
|
|
|
t = T4(1, 2)
|
|
|
|
print(t.foo, t.bar)
|
|
|
|
|
2017-05-29 03:08:14 -04:00
|
|
|
# Try single string with comma field separator
|
2014-12-20 10:37:40 -05:00
|
|
|
# Not implemented so far
|
|
|
|
#T2 = namedtuple("TupComma", "foo,bar")
|
|
|
|
#t = T2(1, 2)
|
2022-07-08 11:48:40 -04:00
|
|
|
|
|
|
|
# Creating an empty namedtuple should not segfault
|
|
|
|
T5 = namedtuple("TupEmpty", [])
|
|
|
|
t = T5()
|
|
|
|
print(t)
|