src/reportlab/lib/set_ops.py
author rptlab
Tue, 30 Apr 2013 14:20:22 +0100
branchpy33
changeset 3721 0c93dd8ff567
parent 3617 ae5744e97c42
child 3794 398ea04239b5
permissions -rw-r--r--
initial changes from 2to3-3.3
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):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    12
    if t is list:
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    13
        return list(S)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    14
    elif t is tuple:
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    15
        return tuple(S)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    16
    elif t is bytes:
1949
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 = []
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    29
    if type(seq1) != type(seq2) and type(seq2) == bytes: seq2 = list(seq2)
1949
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)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    37
    if type(seq1) == list or type(seq2) == list:
1949
6032704b94db Initial check in
rgbecker
parents:
diff changeset
    38
        return unique(list(seq1) + list(seq2))
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    39
    if type(seq1) == tuple or type(seq2) == tuple:
1949
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))