1949
|
1 |
#!/bin/env python
|
|
2 |
#copyright ReportLab Inc. 2000
|
|
3 |
#see license.txt for license details
|
|
4 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/set_ops.py?cvsroot=reportlab
|
|
5 |
#$Header: /tmp/reportlab/reportlab/lib/set_ops.py,v 1.1 2003/06/10 09:59:06 rgbecker Exp $
|
|
6 |
import types
|
|
7 |
import string
|
|
8 |
|
|
9 |
def __set_coerce(t, S):
|
|
10 |
if t is types.ListType:
|
|
11 |
return list(S)
|
|
12 |
elif t is types.TupleType:
|
|
13 |
return tuple(S)
|
|
14 |
elif t is types.StringType:
|
|
15 |
return string.join(S, '')
|
|
16 |
return S
|
|
17 |
|
|
18 |
def unique(seq):
|
|
19 |
result = []
|
|
20 |
for i in seq:
|
|
21 |
if i not in result:
|
|
22 |
result.append(i)
|
|
23 |
return __set_coerce(type(seq), result)
|
|
24 |
|
|
25 |
def intersect(seq1, seq2):
|
|
26 |
result = []
|
|
27 |
if type(seq1) != type(seq2) and type(seq2) == types.StringType: seq2 = list(seq2)
|
|
28 |
for i in seq1:
|
|
29 |
if i in seq2 and i not in result: result.append(i)
|
|
30 |
return __set_coerce(type(seq1), result)
|
|
31 |
|
|
32 |
def union(seq1, seq2):
|
|
33 |
if type(seq1) == type(seq2):
|
|
34 |
return unique(seq1 + seq2)
|
|
35 |
if type(seq1) == types.ListType or type(seq2) == types.ListType:
|
|
36 |
return unique(list(seq1) + list(seq2))
|
|
37 |
if type(seq1) == types.TupleType or type(seq2) == types.TupleType:
|
|
38 |
return unique(tuple(seq1) + tuple(seq2))
|
|
39 |
return unique(list(seq1) + list(seq2))
|