4 warnings
.filterwarnings("ignore", "the sets module is deprecated",
5 DeprecationWarning, "test\.test_sets")
7 import unittest
, operator
, copy
, pickle
, random
8 from sets
import Set
, ImmutableSet
9 from test
import test_support
13 #==============================================================================
15 class TestBasicOps(unittest
.TestCase
):
18 if self
.repr is not None:
19 self
.assertEqual(repr(self
.set), self
.repr)
21 def test_length(self
):
22 self
.assertEqual(len(self
.set), self
.length
)
24 def test_self_equality(self
):
25 self
.assertEqual(self
.set, self
.set)
27 def test_equivalent_equality(self
):
28 self
.assertEqual(self
.set, self
.dup
)
31 self
.assertEqual(self
.set.copy(), self
.dup
)
33 def test_self_union(self
):
34 result
= self
.set | self
.set
35 self
.assertEqual(result
, self
.dup
)
37 def test_empty_union(self
):
38 result
= self
.set | empty_set
39 self
.assertEqual(result
, self
.dup
)
41 def test_union_empty(self
):
42 result
= empty_set | self
.set
43 self
.assertEqual(result
, self
.dup
)
45 def test_self_intersection(self
):
46 result
= self
.set & self
.set
47 self
.assertEqual(result
, self
.dup
)
49 def test_empty_intersection(self
):
50 result
= self
.set & empty_set
51 self
.assertEqual(result
, empty_set
)
53 def test_intersection_empty(self
):
54 result
= empty_set
& self
.set
55 self
.assertEqual(result
, empty_set
)
57 def test_self_symmetric_difference(self
):
58 result
= self
.set ^ self
.set
59 self
.assertEqual(result
, empty_set
)
61 def checkempty_symmetric_difference(self
):
62 result
= self
.set ^ empty_set
63 self
.assertEqual(result
, self
.set)
65 def test_self_difference(self
):
66 result
= self
.set - self
.set
67 self
.assertEqual(result
, empty_set
)
69 def test_empty_difference(self
):
70 result
= self
.set - empty_set
71 self
.assertEqual(result
, self
.dup
)
73 def test_empty_difference_rev(self
):
74 result
= empty_set
- self
.set
75 self
.assertEqual(result
, empty_set
)
77 def test_iteration(self
):
79 self
.assertTrue(v
in self
.values
)
81 def test_pickling(self
):
82 p
= pickle
.dumps(self
.set)
83 copy
= pickle
.loads(p
)
84 self
.assertEqual(self
.set, copy
,
85 "%s != %s" % (self
.set, copy
))
87 #------------------------------------------------------------------------------
89 class TestBasicOpsEmpty(TestBasicOps
):
91 self
.case
= "empty set"
93 self
.set = Set(self
.values
)
94 self
.dup
= Set(self
.values
)
98 #------------------------------------------------------------------------------
100 class TestBasicOpsSingleton(TestBasicOps
):
102 self
.case
= "unit set (number)"
104 self
.set = Set(self
.values
)
105 self
.dup
= Set(self
.values
)
107 self
.repr = "Set([3])"
110 self
.assertTrue(3 in self
.set)
112 def test_not_in(self
):
113 self
.assertTrue(2 not in self
.set)
115 #------------------------------------------------------------------------------
117 class TestBasicOpsTuple(TestBasicOps
):
119 self
.case
= "unit set (tuple)"
120 self
.values
= [(0, "zero")]
121 self
.set = Set(self
.values
)
122 self
.dup
= Set(self
.values
)
124 self
.repr = "Set([(0, 'zero')])"
127 self
.assertTrue((0, "zero") in self
.set)
129 def test_not_in(self
):
130 self
.assertTrue(9 not in self
.set)
132 #------------------------------------------------------------------------------
134 class TestBasicOpsTriple(TestBasicOps
):
136 self
.case
= "triple set"
137 self
.values
= [0, "zero", operator
.add
]
138 self
.set = Set(self
.values
)
139 self
.dup
= Set(self
.values
)
143 #==============================================================================
152 class TestExceptionPropagation(unittest
.TestCase
):
153 """SF 628246: Set constructor should not trap iterator TypeErrors"""
155 def test_instanceWithException(self
):
156 self
.assertRaises(TypeError, Set
, baditer())
158 def test_instancesWithoutException(self
):
159 # All of these iterables should load without exception.
162 Set({'one':1, 'two':2, 'three':3})
167 #==============================================================================
169 class TestSetOfSets(unittest
.TestCase
):
170 def test_constructor(self
):
173 element
= outer
.pop()
174 self
.assertEqual(type(element
), ImmutableSet
)
175 outer
.add(inner
) # Rebuild set of sets with .add method
177 self
.assertEqual(outer
, Set()) # Verify that remove worked
178 outer
.discard(inner
) # Absence of KeyError indicates working fine
180 #==============================================================================
182 class TestBinaryOps(unittest
.TestCase
):
184 self
.set = Set((2, 4, 6))
186 def test_eq(self
): # SF bug 643115
187 self
.assertEqual(self
.set, Set({2:1,4:3,6:5}))
189 def test_union_subset(self
):
190 result
= self
.set |
Set([2])
191 self
.assertEqual(result
, Set((2, 4, 6)))
193 def test_union_superset(self
):
194 result
= self
.set |
Set([2, 4, 6, 8])
195 self
.assertEqual(result
, Set([2, 4, 6, 8]))
197 def test_union_overlap(self
):
198 result
= self
.set |
Set([3, 4, 5])
199 self
.assertEqual(result
, Set([2, 3, 4, 5, 6]))
201 def test_union_non_overlap(self
):
202 result
= self
.set |
Set([8])
203 self
.assertEqual(result
, Set([2, 4, 6, 8]))
205 def test_intersection_subset(self
):
206 result
= self
.set & Set((2, 4))
207 self
.assertEqual(result
, Set((2, 4)))
209 def test_intersection_superset(self
):
210 result
= self
.set & Set([2, 4, 6, 8])
211 self
.assertEqual(result
, Set([2, 4, 6]))
213 def test_intersection_overlap(self
):
214 result
= self
.set & Set([3, 4, 5])
215 self
.assertEqual(result
, Set([4]))
217 def test_intersection_non_overlap(self
):
218 result
= self
.set & Set([8])
219 self
.assertEqual(result
, empty_set
)
221 def test_sym_difference_subset(self
):
222 result
= self
.set ^
Set((2, 4))
223 self
.assertEqual(result
, Set([6]))
225 def test_sym_difference_superset(self
):
226 result
= self
.set ^
Set((2, 4, 6, 8))
227 self
.assertEqual(result
, Set([8]))
229 def test_sym_difference_overlap(self
):
230 result
= self
.set ^
Set((3, 4, 5))
231 self
.assertEqual(result
, Set([2, 3, 5, 6]))
233 def test_sym_difference_non_overlap(self
):
234 result
= self
.set ^
Set([8])
235 self
.assertEqual(result
, Set([2, 4, 6, 8]))
238 a
, b
= Set('a'), Set('b')
239 self
.assertRaises(TypeError, cmp, a
, b
)
241 # You can view this as a buglet: cmp(a, a) does not raise TypeError,
242 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
243 # which Python thinks is good enough to synthesize a cmp() result
244 # without calling __cmp__.
245 self
.assertEqual(cmp(a
, a
), 0)
247 self
.assertRaises(TypeError, cmp, a
, 12)
248 self
.assertRaises(TypeError, cmp, "abc", a
)
250 def test_inplace_on_self(self
):
253 self
.assertEqual(t
, self
.set)
255 self
.assertEqual(t
, self
.set)
257 self
.assertEqual(len(t
), 0)
260 self
.assertEqual(len(t
), 0)
263 #==============================================================================
265 class TestUpdateOps(unittest
.TestCase
):
267 self
.set = Set((2, 4, 6))
269 def test_union_subset(self
):
271 self
.assertEqual(self
.set, Set((2, 4, 6)))
273 def test_union_superset(self
):
274 self
.set |
= Set([2, 4, 6, 8])
275 self
.assertEqual(self
.set, Set([2, 4, 6, 8]))
277 def test_union_overlap(self
):
278 self
.set |
= Set([3, 4, 5])
279 self
.assertEqual(self
.set, Set([2, 3, 4, 5, 6]))
281 def test_union_non_overlap(self
):
283 self
.assertEqual(self
.set, Set([2, 4, 6, 8]))
285 def test_union_method_call(self
):
286 self
.set.union_update(Set([3, 4, 5]))
287 self
.assertEqual(self
.set, Set([2, 3, 4, 5, 6]))
289 def test_intersection_subset(self
):
290 self
.set &= Set((2, 4))
291 self
.assertEqual(self
.set, Set((2, 4)))
293 def test_intersection_superset(self
):
294 self
.set &= Set([2, 4, 6, 8])
295 self
.assertEqual(self
.set, Set([2, 4, 6]))
297 def test_intersection_overlap(self
):
298 self
.set &= Set([3, 4, 5])
299 self
.assertEqual(self
.set, Set([4]))
301 def test_intersection_non_overlap(self
):
303 self
.assertEqual(self
.set, empty_set
)
305 def test_intersection_method_call(self
):
306 self
.set.intersection_update(Set([3, 4, 5]))
307 self
.assertEqual(self
.set, Set([4]))
309 def test_sym_difference_subset(self
):
310 self
.set ^
= Set((2, 4))
311 self
.assertEqual(self
.set, Set([6]))
313 def test_sym_difference_superset(self
):
314 self
.set ^
= Set((2, 4, 6, 8))
315 self
.assertEqual(self
.set, Set([8]))
317 def test_sym_difference_overlap(self
):
318 self
.set ^
= Set((3, 4, 5))
319 self
.assertEqual(self
.set, Set([2, 3, 5, 6]))
321 def test_sym_difference_non_overlap(self
):
323 self
.assertEqual(self
.set, Set([2, 4, 6, 8]))
325 def test_sym_difference_method_call(self
):
326 self
.set.symmetric_difference_update(Set([3, 4, 5]))
327 self
.assertEqual(self
.set, Set([2, 3, 5, 6]))
329 def test_difference_subset(self
):
330 self
.set -= Set((2, 4))
331 self
.assertEqual(self
.set, Set([6]))
333 def test_difference_superset(self
):
334 self
.set -= Set((2, 4, 6, 8))
335 self
.assertEqual(self
.set, Set([]))
337 def test_difference_overlap(self
):
338 self
.set -= Set((3, 4, 5))
339 self
.assertEqual(self
.set, Set([2, 6]))
341 def test_difference_non_overlap(self
):
343 self
.assertEqual(self
.set, Set([2, 4, 6]))
345 def test_difference_method_call(self
):
346 self
.set.difference_update(Set([3, 4, 5]))
347 self
.assertEqual(self
.set, Set([2, 6]))
349 #==============================================================================
351 class TestMutate(unittest
.TestCase
):
353 self
.values
= ["a", "b", "c"]
354 self
.set = Set(self
.values
)
356 def test_add_present(self
):
358 self
.assertEqual(self
.set, Set("abc"))
360 def test_add_absent(self
):
362 self
.assertEqual(self
.set, Set("abcd"))
364 def test_add_until_full(self
):
367 for v
in self
.values
:
370 self
.assertEqual(len(tmp
), expected_len
)
371 self
.assertEqual(tmp
, self
.set)
373 def test_remove_present(self
):
375 self
.assertEqual(self
.set, Set("ac"))
377 def test_remove_absent(self
):
380 self
.fail("Removing missing element should have raised LookupError")
384 def test_remove_until_empty(self
):
385 expected_len
= len(self
.set)
386 for v
in self
.values
:
389 self
.assertEqual(len(self
.set), expected_len
)
391 def test_discard_present(self
):
392 self
.set.discard("c")
393 self
.assertEqual(self
.set, Set("ab"))
395 def test_discard_absent(self
):
396 self
.set.discard("d")
397 self
.assertEqual(self
.set, Set("abc"))
399 def test_clear(self
):
401 self
.assertEqual(len(self
.set), 0)
406 popped
[self
.set.pop()] = None
407 self
.assertEqual(len(popped
), len(self
.values
))
408 for v
in self
.values
:
409 self
.assertTrue(v
in popped
)
411 def test_update_empty_tuple(self
):
412 self
.set.union_update(())
413 self
.assertEqual(self
.set, Set(self
.values
))
415 def test_update_unit_tuple_overlap(self
):
416 self
.set.union_update(("a",))
417 self
.assertEqual(self
.set, Set(self
.values
))
419 def test_update_unit_tuple_non_overlap(self
):
420 self
.set.union_update(("a", "z"))
421 self
.assertEqual(self
.set, Set(self
.values
+ ["z"]))
423 #==============================================================================
425 class TestSubsets(unittest
.TestCase
):
427 case2method
= {"<=": "issubset",
431 reverse
= {"==": "==",
439 def test_issubset(self
):
442 for case
in "!=", "==", "<", "<=", ">", ">=":
443 expected
= case
in self
.cases
444 # Test the binary infix spelling.
445 result
= eval("x" + case
+ "y", locals())
446 self
.assertEqual(result
, expected
)
447 # Test the "friendly" method-name spelling, if one exists.
448 if case
in TestSubsets
.case2method
:
449 method
= getattr(x
, TestSubsets
.case2method
[case
])
451 self
.assertEqual(result
, expected
)
453 # Now do the same for the operands reversed.
454 rcase
= TestSubsets
.reverse
[case
]
455 result
= eval("y" + rcase
+ "x", locals())
456 self
.assertEqual(result
, expected
)
457 if rcase
in TestSubsets
.case2method
:
458 method
= getattr(y
, TestSubsets
.case2method
[rcase
])
460 self
.assertEqual(result
, expected
)
461 #------------------------------------------------------------------------------
463 class TestSubsetEqualEmpty(TestSubsets
):
467 cases
= "==", "<=", ">="
469 #------------------------------------------------------------------------------
471 class TestSubsetEqualNonEmpty(TestSubsets
):
475 cases
= "==", "<=", ">="
477 #------------------------------------------------------------------------------
479 class TestSubsetEmptyNonEmpty(TestSubsets
):
482 name
= "one empty, one non-empty"
483 cases
= "!=", "<", "<="
485 #------------------------------------------------------------------------------
487 class TestSubsetPartial(TestSubsets
):
490 name
= "one a non-empty proper subset of other"
491 cases
= "!=", "<", "<="
493 #------------------------------------------------------------------------------
495 class TestSubsetNonOverlap(TestSubsets
):
498 name
= "neither empty, neither contains"
501 #==============================================================================
503 class TestOnlySetsInBinaryOps(unittest
.TestCase
):
505 def test_eq_ne(self
):
506 # Unlike the others, this is testing that == and != *are* allowed.
507 self
.assertEqual(self
.other
== self
.set, False)
508 self
.assertEqual(self
.set == self
.other
, False)
509 self
.assertEqual(self
.other
!= self
.set, True)
510 self
.assertEqual(self
.set != self
.other
, True)
512 def test_ge_gt_le_lt(self
):
513 self
.assertRaises(TypeError, lambda: self
.set < self
.other
)
514 self
.assertRaises(TypeError, lambda: self
.set <= self
.other
)
515 self
.assertRaises(TypeError, lambda: self
.set > self
.other
)
516 self
.assertRaises(TypeError, lambda: self
.set >= self
.other
)
518 self
.assertRaises(TypeError, lambda: self
.other
< self
.set)
519 self
.assertRaises(TypeError, lambda: self
.other
<= self
.set)
520 self
.assertRaises(TypeError, lambda: self
.other
> self
.set)
521 self
.assertRaises(TypeError, lambda: self
.other
>= self
.set)
523 def test_union_update_operator(self
):
525 self
.set |
= self
.other
529 self
.fail("expected TypeError")
531 def test_union_update(self
):
532 if self
.otherIsIterable
:
533 self
.set.union_update(self
.other
)
535 self
.assertRaises(TypeError, self
.set.union_update
, self
.other
)
537 def test_union(self
):
538 self
.assertRaises(TypeError, lambda: self
.set | self
.other
)
539 self
.assertRaises(TypeError, lambda: self
.other | self
.set)
540 if self
.otherIsIterable
:
541 self
.set.union(self
.other
)
543 self
.assertRaises(TypeError, self
.set.union
, self
.other
)
545 def test_intersection_update_operator(self
):
547 self
.set &= self
.other
551 self
.fail("expected TypeError")
553 def test_intersection_update(self
):
554 if self
.otherIsIterable
:
555 self
.set.intersection_update(self
.other
)
557 self
.assertRaises(TypeError,
558 self
.set.intersection_update
,
561 def test_intersection(self
):
562 self
.assertRaises(TypeError, lambda: self
.set & self
.other
)
563 self
.assertRaises(TypeError, lambda: self
.other
& self
.set)
564 if self
.otherIsIterable
:
565 self
.set.intersection(self
.other
)
567 self
.assertRaises(TypeError, self
.set.intersection
, self
.other
)
569 def test_sym_difference_update_operator(self
):
571 self
.set ^
= self
.other
575 self
.fail("expected TypeError")
577 def test_sym_difference_update(self
):
578 if self
.otherIsIterable
:
579 self
.set.symmetric_difference_update(self
.other
)
581 self
.assertRaises(TypeError,
582 self
.set.symmetric_difference_update
,
585 def test_sym_difference(self
):
586 self
.assertRaises(TypeError, lambda: self
.set ^ self
.other
)
587 self
.assertRaises(TypeError, lambda: self
.other ^ self
.set)
588 if self
.otherIsIterable
:
589 self
.set.symmetric_difference(self
.other
)
591 self
.assertRaises(TypeError, self
.set.symmetric_difference
, self
.other
)
593 def test_difference_update_operator(self
):
595 self
.set -= self
.other
599 self
.fail("expected TypeError")
601 def test_difference_update(self
):
602 if self
.otherIsIterable
:
603 self
.set.difference_update(self
.other
)
605 self
.assertRaises(TypeError,
606 self
.set.difference_update
,
609 def test_difference(self
):
610 self
.assertRaises(TypeError, lambda: self
.set - self
.other
)
611 self
.assertRaises(TypeError, lambda: self
.other
- self
.set)
612 if self
.otherIsIterable
:
613 self
.set.difference(self
.other
)
615 self
.assertRaises(TypeError, self
.set.difference
, self
.other
)
617 #------------------------------------------------------------------------------
619 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps
):
621 self
.set = Set((1, 2, 3))
623 self
.otherIsIterable
= False
625 #------------------------------------------------------------------------------
627 class TestOnlySetsDict(TestOnlySetsInBinaryOps
):
629 self
.set = Set((1, 2, 3))
630 self
.other
= {1:2, 3:4}
631 self
.otherIsIterable
= True
633 #------------------------------------------------------------------------------
635 class TestOnlySetsOperator(TestOnlySetsInBinaryOps
):
637 self
.set = Set((1, 2, 3))
638 self
.other
= operator
.add
639 self
.otherIsIterable
= False
641 #------------------------------------------------------------------------------
643 class TestOnlySetsTuple(TestOnlySetsInBinaryOps
):
645 self
.set = Set((1, 2, 3))
646 self
.other
= (2, 4, 6)
647 self
.otherIsIterable
= True
649 #------------------------------------------------------------------------------
651 class TestOnlySetsString(TestOnlySetsInBinaryOps
):
653 self
.set = Set((1, 2, 3))
655 self
.otherIsIterable
= True
657 #------------------------------------------------------------------------------
659 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps
):
662 for i
in xrange(0, 10, 2):
664 self
.set = Set((1, 2, 3))
666 self
.otherIsIterable
= True
668 #------------------------------------------------------------------------------
670 class TestOnlySetsofSets(TestOnlySetsInBinaryOps
):
672 self
.set = Set((1, 2, 3))
673 self
.other
= [Set('ab'), ImmutableSet('cd')]
674 self
.otherIsIterable
= True
676 #==============================================================================
678 class TestCopying(unittest
.TestCase
):
681 dup
= self
.set.copy()
682 dup_list
= list(dup
); dup_list
.sort()
683 set_list
= list(self
.set); set_list
.sort()
684 self
.assertEqual(len(dup_list
), len(set_list
))
685 for i
in range(len(dup_list
)):
686 self
.assertTrue(dup_list
[i
] is set_list
[i
])
688 def test_deep_copy(self
):
689 dup
= copy
.deepcopy(self
.set)
690 ##print type(dup), repr(dup)
691 dup_list
= list(dup
); dup_list
.sort()
692 set_list
= list(self
.set); set_list
.sort()
693 self
.assertEqual(len(dup_list
), len(set_list
))
694 for i
in range(len(dup_list
)):
695 self
.assertEqual(dup_list
[i
], set_list
[i
])
697 #------------------------------------------------------------------------------
699 class TestCopyingEmpty(TestCopying
):
703 #------------------------------------------------------------------------------
705 class TestCopyingSingleton(TestCopying
):
707 self
.set = Set(["hello"])
709 #------------------------------------------------------------------------------
711 class TestCopyingTriple(TestCopying
):
713 self
.set = Set(["zero", 0, None])
715 #------------------------------------------------------------------------------
717 class TestCopyingTuple(TestCopying
):
719 self
.set = Set([(1, 2)])
721 #------------------------------------------------------------------------------
723 class TestCopyingNested(TestCopying
):
725 self
.set = Set([((1, 2), (3, 4))])
727 #==============================================================================
729 class TestIdentities(unittest
.TestCase
):
731 self
.a
= Set([random
.randrange(100) for i
in xrange(50)])
732 self
.b
= Set([random
.randrange(100) for i
in xrange(50)])
734 def test_binopsVsSubsets(self
):
735 a
, b
= self
.a
, self
.b
736 self
.assertTrue(a
- b
<= a
)
737 self
.assertTrue(b
- a
<= b
)
738 self
.assertTrue(a
& b
<= a
)
739 self
.assertTrue(a
& b
<= b
)
740 self
.assertTrue(a | b
>= a
)
741 self
.assertTrue(a | b
>= b
)
742 self
.assertTrue(a ^ b
<= a | b
)
744 def test_commutativity(self
):
745 a
, b
= self
.a
, self
.b
746 self
.assertEqual(a
&b
, b
&a
)
747 self
.assertEqual(a|b
, b|a
)
748 self
.assertEqual(a^b
, b^a
)
750 self
.assertNotEqual(a
-b
, b
-a
)
752 def test_reflexsive_relations(self
):
753 a
, zero
= self
.a
, Set()
754 self
.assertEqual(a ^ a
, zero
)
755 self
.assertEqual(a
- a
, zero
)
756 self
.assertEqual(a | a
, a
)
757 self
.assertEqual(a
& a
, a
)
758 self
.assertTrue(a
<= a
)
759 self
.assertTrue(a
>= a
)
760 self
.assertTrue(a
== a
)
762 def test_summations(self
):
763 # check that sums of parts equal the whole
764 a
, b
= self
.a
, self
.b
765 self
.assertEqual((a
-b
)|
(a
&b
)|
(b
-a
), a|b
)
766 self
.assertEqual((a
&b
)|
(a^b
), a|b
)
767 self
.assertEqual(a|
(b
-a
), a|b
)
768 self
.assertEqual((a
-b
)|b
, a|b
)
769 self
.assertEqual((a
-b
)|
(a
&b
), a
)
770 self
.assertEqual((b
-a
)|
(a
&b
), b
)
771 self
.assertEqual((a
-b
)|
(b
-a
), a^b
)
773 def test_exclusion(self
):
774 # check that inverse operations do not overlap
775 a
, b
, zero
= self
.a
, self
.b
, Set()
776 self
.assertEqual((a
-b
)&b
, zero
)
777 self
.assertEqual((b
-a
)&a
, zero
)
778 self
.assertEqual((a
&b
)&(a^b
), zero
)
780 def test_cardinality_relations(self
):
781 a
, b
= self
.a
, self
.b
782 self
.assertEqual(len(a
), len(a
-b
) + len(a
&b
))
783 self
.assertEqual(len(b
), len(b
-a
) + len(a
&b
))
784 self
.assertEqual(len(a^b
), len(a
-b
) + len(b
-a
))
785 self
.assertEqual(len(a|b
), len(a
-b
) + len(a
&b
) + len(b
-a
))
786 self
.assertEqual(len(a^b
) + len(a
&b
), len(a|b
))
788 #==============================================================================
791 Example from the Library Reference: Doc/lib/libsets.tex
793 >>> from sets import Set as Base # override _repr to get sorted output
796 ... return Base._repr(self, sorted=True)
797 >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
798 >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
799 >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
800 >>> employees = engineers | programmers | managers # union
801 >>> engineering_management = engineers & managers # intersection
802 >>> fulltime_management = managers - engineers - programmers # difference
803 >>> engineers.add('Marvin')
805 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
806 >>> employees.issuperset(engineers) # superset test
808 >>> employees.union_update(engineers) # update from another set
809 >>> employees.issuperset(engineers)
811 >>> for group in [engineers, programmers, managers, employees]:
812 ... group.discard('Susan') # unconditionally remove element
815 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
816 Set(['Jack', 'Janice', 'Sam'])
817 Set(['Jack', 'Jane', 'Zack'])
818 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack'])
821 #==============================================================================
823 __test__
= {'libreftest' : libreftest
}
825 def test_main(verbose
=None):
827 from test
import test_sets
828 test_support
.run_unittest(
830 TestExceptionPropagation
,
832 TestBasicOpsSingleton
,
838 TestSubsetEqualEmpty
,
839 TestSubsetEqualNonEmpty
,
840 TestSubsetEmptyNonEmpty
,
842 TestSubsetNonOverlap
,
845 TestOnlySetsOperator
,
848 TestOnlySetsGenerator
,
851 TestCopyingSingleton
,
856 doctest
.DocTestSuite(test_sets
),
859 if __name__
== "__main__":
860 test_main(verbose
=True)