Module csvutils :: Class SequenceTransformer
[hide private]
[frames] | no frames]

Class SequenceTransformer

source code

    object --+    
             |    
RowTransformer --+
                 |
                SequenceTransformer

A RowTransformer that expects and returns rows as sequences.

Examples:
>>> import csv
>>> rows = list(csv.reader(["1,3.34,4-3.2j,John",
...                         "4,4,4,4",
...                         "0,-1.1,3.4,None"]))
>>> # by default, SequenceTransformer returns each row as is
>>> list(SequenceTransformer()(rows)) == rows
True
>>> # transform and return the first two columns only
>>> for row in SequenceTransformer(int,float)(rows):
...    print row
[1, 3.3399999999999999]
[4, 4.0]
[0, -1.1000000000000001]
>>> # as before, but keep the rest columns too
>>> for row in SequenceTransformer(int, float, default=None)(rows):
...    print row
[1, 3.3399999999999999, '4-3.2j', 'John']
[4, 4.0, '4', '4']
[0, -1.1000000000000001, '3.4', 'None']
>>> # as before, but in reverse column order
>>> for row in SequenceTransformer(int, float, default=None,
...                                include=reversed(xrange(4)))(rows):
...    print row
['John', '4-3.2j', 3.3399999999999999, 1]
['4', '4', 4.0, 4]
['None', '3.4', -1.1000000000000001, 0]
>>> # transform the second column and leave the rest as is
>>> for row in SequenceTransformer((1,float), default=None)(rows):
...    print row
['1', 3.3399999999999999, '4-3.2j', 'John']
['4', 4.0, '4', '4']
['0', -1.1000000000000001, '3.4', 'None']
>>> # transform and return the 4nd and the 2th column, in this order
>>> for row in SequenceTransformer((3,str),(1,float))(rows):
...    print row
['John', 3.3399999999999999]
['4', 4.0]
['None', -1.1000000000000001]
>>> # exclude the 4th column and eval() the rest (XXX: Use eval for trusted data only)
>>> for row in SequenceTransformer(default=eval, exclude=[3])(rows):
...    print row
[1, 3.3399999999999999, (4-3.2000000000000002j)]
[4, 4, 4]
[0, -1.1000000000000001, 3.3999999999999999]


Instance Methods [hide private]
 
__init__(self, *adaptors, **kwds)
Specifies what transformations to apply to each row.
source code
 
__call__(self, rows)
Transform the given rows by this transformer.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *adaptors, **kwds)
(Constructor)

source code 
Specifies what transformations to apply to each row.
Parameters:
  • adaptors - The adaptors for selected columns. The i-th adaptor can be:
    • None: row[i] will be left as is.
    • A callable f(x): row[i] will be transformed by f to f(row[i]).
    • A pair (j,A): row[j] will be transformed by adaptor A, where A can be None or a callable f(x) as above. i is ignored in this case.
  • include - It can be:
    • An iterable of indices: Only the items at the respective columns are included (except for those that are also in exclude).
    • A positive integer N: shortcut for xrange(N).
  • default, exclude - See RowTransformer.__init__
Overrides: RowTransformer.__init__

__call__(self, rows)
(Call operator)

source code 
Transform the given rows by this transformer.
Parameters:
  • rows - An iterable of sequences.
Returns:
An iterator over the transformed rows as lists.
Overrides: RowTransformer.__call__