src/reportlab/lib/set_ops.py
author robin
Thu, 27 Sep 2012 14:39:39 +0000
changeset 3617 ae5744e97c42
parent 3029 eded59f94021
child 3721 0c93dd8ff567
permissions -rw-r--r--
reportlab: copyright date changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     1
#!/bin/env python
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3029
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2012
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     3
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1949
diff changeset
     4
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/set_ops.py
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     5
__version__=''' $Id$ '''
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     6
__doc__="""From before Python had a Set class..."""
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     7
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     8
import types
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     9
import string
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    10
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    11
def __set_coerce(t, S):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    12
    if t is types.ListType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    13
        return list(S)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    14
    elif t is types.TupleType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    15
        return tuple(S)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    16
    elif t is types.StringType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    17
        return string.join(S, '')
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    18
    return S
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    19
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    20
def unique(seq):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    21
    result = []
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    22
    for i in seq:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    23
        if i not in result:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    24
            result.append(i)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    25
    return __set_coerce(type(seq), result)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    26
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    27
def intersect(seq1, seq2):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    28
    result = []
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    29
    if type(seq1) != type(seq2) and type(seq2) == types.StringType: seq2 = list(seq2)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    30
    for i in seq1:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    31
        if i in seq2 and i not in result: result.append(i)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    32
    return __set_coerce(type(seq1), result)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    33
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    34
def union(seq1, seq2):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    35
    if type(seq1) == type(seq2):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    36
        return unique(seq1 + seq2)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    37
    if type(seq1) == types.ListType or type(seq2) == types.ListType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    38
        return unique(list(seq1) + list(seq2))
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    39
    if type(seq1) == types.TupleType or type(seq2) == types.TupleType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    40
        return unique(tuple(seq1) + tuple(seq2))
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    41
    return unique(list(seq1) + list(seq2))