2015-08-21 11:56:14 +01:00
|
|
|
# test set binary operations
|
|
|
|
|
2014-01-12 23:37:45 +00:00
|
|
|
sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
|
|
|
|
for s in sets:
|
|
|
|
for t in sets:
|
2014-04-07 05:00:03 +01:00
|
|
|
print(sorted(s), '|', sorted(t), '=', sorted(s | t))
|
|
|
|
print(sorted(s), '^', sorted(t), '=', sorted(s ^ t))
|
|
|
|
print(sorted(s), '&', sorted(t), '=', sorted(s & t))
|
|
|
|
print(sorted(s), '-', sorted(t), '=', sorted(s - t))
|
2014-01-12 23:37:45 +00:00
|
|
|
u = s.copy()
|
|
|
|
u |= t
|
2014-04-07 05:00:03 +01:00
|
|
|
print(sorted(s), "|=", sorted(t), '-->', sorted(u))
|
2014-01-12 23:37:45 +00:00
|
|
|
u = s.copy()
|
|
|
|
u ^= t
|
2014-04-07 05:00:03 +01:00
|
|
|
print(sorted(s), "^=", sorted(t), '-->', sorted(u))
|
2014-01-12 23:37:45 +00:00
|
|
|
u = s.copy()
|
|
|
|
u &= t
|
2014-04-07 05:00:03 +01:00
|
|
|
print(sorted(s), "&=", sorted(t), "-->", sorted(u))
|
2014-01-12 23:37:45 +00:00
|
|
|
u = s.copy()
|
|
|
|
u -= t
|
2014-04-07 05:00:03 +01:00
|
|
|
print(sorted(s), "-=", sorted(t), "-->", sorted(u))
|
2014-01-12 23:09:10 +00:00
|
|
|
|
2014-04-07 05:00:03 +01:00
|
|
|
print(sorted(s), '==', sorted(t), '=', s == t)
|
|
|
|
print(sorted(s), '!=', sorted(t), '=', s != t)
|
|
|
|
print(sorted(s), '>', sorted(t), '=', s > t)
|
|
|
|
print(sorted(s), '>=', sorted(t), '=', s >= t)
|
|
|
|
print(sorted(s), '<', sorted(t), '=', s < t)
|
|
|
|
print(sorted(s), '<=', sorted(t), '=', s <= t)
|
2015-08-21 11:56:14 +01:00
|
|
|
|
|
|
|
print(set('abc') == 1)
|
|
|
|
|
2017-02-02 12:36:53 +00:00
|
|
|
# make sure inplace operators modify the set
|
|
|
|
|
|
|
|
s1 = s2 = set('abc')
|
|
|
|
s1 |= set('ad')
|
|
|
|
print(s1 is s2, len(s1))
|
|
|
|
|
|
|
|
s1 = s2 = set('abc')
|
|
|
|
s1 ^= set('ad')
|
|
|
|
print(s1 is s2, len(s1))
|
|
|
|
|
|
|
|
s1 = s2 = set('abc')
|
|
|
|
s1 &= set('ad')
|
|
|
|
print(s1 is s2, len(s1))
|
|
|
|
|
|
|
|
s1 = s2 = set('abc')
|
|
|
|
s1 -= set('ad')
|
|
|
|
print(s1 is s2, len(s1))
|
|
|
|
|
2017-10-03 07:56:27 +01:00
|
|
|
# RHS must be a set
|
|
|
|
try:
|
|
|
|
print(set('12') >= '1')
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
|
|
|
|
|
|
|
# RHS must be a set
|
|
|
|
try:
|
|
|
|
print(set('12') <= '123')
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
|
|
|
|
2017-02-02 12:36:53 +00:00
|
|
|
# unsupported operator
|
2017-12-19 02:59:54 +00:00
|
|
|
try:
|
|
|
|
set('abc') * set('abc')
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
|
|
|
|
|
|
|
# unsupported operator with RHS not a set
|
2015-08-21 11:56:14 +01:00
|
|
|
try:
|
|
|
|
set('abc') * 2
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|