File: distinct.py

package info (click to toggle)
python-agate 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (44 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from agate import utils


def distinct(self, key=None):
    """
    Create a new table with only unique rows.

    :param key:
        Either the name of a single column to use to identify unique rows, a
        sequence of such column names, a :class:`function` that takes a
        row and returns a value to identify unique rows, or `None`, in
        which case the entire row will be checked for uniqueness.
    :returns:
        A new :class:`.Table`.
    """
    key_is_row_function = hasattr(key, '__call__')
    key_is_sequence = utils.issequence(key)

    uniques = []
    rows = []

    if self._row_names is not None:
        row_names = []
    else:
        row_names = None

    for i, row in enumerate(self._rows):
        if key_is_row_function:
            k = key(row)
        elif key_is_sequence:
            k = (row[j] for j in key)
        elif key is None:
            k = tuple(row)
        else:
            k = row[key]

        if k not in uniques:
            uniques.append(k)
            rows.append(row)

            if self._row_names is not None:
                row_names.append(self._row_names[i])

    return self._fork(rows, row_names=row_names)