src/reportlab/lib/set_ops.py
author rgbecker
Wed, 03 Sep 2008 16:10:51 +0000
changeset 2964 32352db0d71e
parent 2332 reportlab/lib/set_ops.py@2a7ab4405e18
child 3029 eded59f94021
permissions -rw-r--r--
reportlab-2.2: second stage of major re-org
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     1
#!/bin/env python
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1949
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2004
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
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     5
import types
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     6
import string
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     7
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     8
def __set_coerce(t, S):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
     9
    if t is types.ListType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    10
        return list(S)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    11
    elif t is types.TupleType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    12
        return tuple(S)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    13
    elif t is types.StringType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    14
        return string.join(S, '')
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    15
    return S
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    16
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    17
def unique(seq):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    18
    result = []
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    19
    for i in seq:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    20
        if i not in result:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    21
            result.append(i)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    22
    return __set_coerce(type(seq), result)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    23
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    24
def intersect(seq1, seq2):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    25
    result = []
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    26
    if type(seq1) != type(seq2) and type(seq2) == types.StringType: seq2 = list(seq2)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    27
    for i in seq1:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    28
        if i in seq2 and i not in result: result.append(i)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    29
    return __set_coerce(type(seq1), result)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    30
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    31
def union(seq1, seq2):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    32
    if type(seq1) == type(seq2):
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    33
        return unique(seq1 + seq2)
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    34
    if type(seq1) == types.ListType or type(seq2) == types.ListType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    35
        return unique(list(seq1) + list(seq2))
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    36
    if type(seq1) == types.TupleType or type(seq2) == types.TupleType:
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    37
        return unique(tuple(seq1) + tuple(seq2))
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    38
    return unique(list(seq1) + list(seq2))