1949
|
1 |
#!/bin/env python
|
2332
|
2 |
#Copyright ReportLab Europe Ltd. 2000-2004
|
1949
|
3 |
#see license.txt for license details
|
2332
|
4 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/set_ops.py
|
3029
|
5 |
__version__=''' $Id$ '''
|
|
6 |
__doc__="""From before Python had a Set class..."""
|
|
7 |
|
1949
|
8 |
import types
|
|
9 |
import string
|
|
10 |
|
|
11 |
def __set_coerce(t, S):
|
|
12 |
if t is types.ListType:
|
|
13 |
return list(S)
|
|
14 |
elif t is types.TupleType:
|
|
15 |
return tuple(S)
|
|
16 |
elif t is types.StringType:
|
|
17 |
return string.join(S, '')
|
|
18 |
return S
|
|
19 |
|
|
20 |
def unique(seq):
|
|
21 |
result = []
|
|
22 |
for i in seq:
|
|
23 |
if i not in result:
|
|
24 |
result.append(i)
|
|
25 |
return __set_coerce(type(seq), result)
|
|
26 |
|
|
27 |
def intersect(seq1, seq2):
|
|
28 |
result = []
|
|
29 |
if type(seq1) != type(seq2) and type(seq2) == types.StringType: seq2 = list(seq2)
|
|
30 |
for i in seq1:
|
|
31 |
if i in seq2 and i not in result: result.append(i)
|
|
32 |
return __set_coerce(type(seq1), result)
|
|
33 |
|
|
34 |
def union(seq1, seq2):
|
|
35 |
if type(seq1) == type(seq2):
|
|
36 |
return unique(seq1 + seq2)
|
|
37 |
if type(seq1) == types.ListType or type(seq2) == types.ListType:
|
|
38 |
return unique(list(seq1) + list(seq2))
|
|
39 |
if type(seq1) == types.TupleType or type(seq2) == types.TupleType:
|
|
40 |
return unique(tuple(seq1) + tuple(seq2))
|
|
41 |
return unique(list(seq1) + list(seq2))
|