polymatheia.transform
The transform
module implements a number of transform classes.
All transform classes use the following transform expression language and are designed to work with
NavigableDict
. Each transform is structured as follows:
('operator', 'parameter'[, 'parameter', ...])
. The number of parameters depends on the operator:
Static:
('static', target, value)
: The staticvalue
is stored at the location specified by the dotted pathtarget
.Copy:
('copy', target, source)
: The value specified by the dotted pathsource
is stored at the location specified by the dotted pathtarget
.Fill:
('fill', target, value)
: If the value specified by the dotted pathtarget
isNone
, then this is replaced by thevalue
, otherwise it is left untouched.Split:
('split', target, splitter, source): The value specified by the dotted path ``source
is split and stored at the location specified by the dotted pathtarget
. If the value is alist
, then the list is split into its elements. If the value is astr
, then it is split usingsplitter`. Because this will produce more than one value in the transformation result, the ``target
can contain the string{}
, which is replaced with the index of each split value (starting at 1).Combine:
('combine', target, source1, ...)
: The values specified by the dotted pathssource1
(and optionallysource2
,source3
,…) are combined into a list and stored at the location specified by the dotted pathtarget
.Join:
('join', target, joiner, source1, ...)
: The values specified by the dotted pathssource1
(and optionallysource2
,source3
,…) are joined by thejoiner
and the result stored at the location specified by the dotted pathtarget
. If only onesource
is specified, this is treated as alist
and the values of thelist
joined usingjoiner
. If multiplesource
s are specified, then the values returned by each dottedsource
path are returned.sequence:
('sequence', expression1, expression2, ...)
: Applies the transform expressions in sequence. When using the sequence transformation, this should be the only top-level transformation being applied, otherwise the results cannot be guaranteed.parallel:
('multiple', expression1, expression2, ...)
: Applies the transform expressions in parallel. Only needed if you want to convert multiple expressions in parallel inside asequence
transform. Parallel transforms must not be mixed with other transforms at the same level.custom:
('custom', target, callable)
: Applies the custom transformation defined by thecallable
and stores the result intarget
.
- class polymatheia.transform.RecordsTransform(records, mappings)
The
RecordsTransform
provides a record transformation iterator container.The actual transformation is performed using a
Transform
that is applied to each record in the source iterator.- __init__(records, mappings)
Create a new
RecordsTransform
.- Parameters:
records – The source records to iterate over and transform
mappings (
list
) – The mappings that are applied to each record
- __iter__()
Return this class:~polymatheia.transform.RecordsTransform as the iterator.
- __weakref__
list of weak references to the object (if defined)
- class polymatheia.transform.RecordsTransformIterator(records, mappings)
The
RecordsTransformIterator
provides a record transformation iterator.The actual transformation is performed using a
Transform
that is applied to each record in the source iterator.- __init__(records, mappings)
Create a new
RecordsTransformIterator
.- Parameters:
records – The source records to iterate over and transform
mappings (
list
) – The mappings that are applied to each record
- __iter__()
Return this class:~polymatheia.transform.RecordsTransformIterator as the iterator.
- __next__()
Transform and return the next
NavigableDict
record.- Returns:
The transformed record
- Return type:
- Raises:
StopIteration – If no more records are available
- __weakref__
list of weak references to the object (if defined)
- class polymatheia.transform.Transform(mapping)
The
Transform
is a callable transformation forNavigableDict
.- __call__(record)
Transform the
record
according to the mappings of thisTransform
.- Parameters:
record (
NavigableDict
) – The record to transform- Returns:
The transformed record
- Return type:
- __init__(mapping)
Create a new
Transform
.- Parameters:
mapping (
tuple
orlist
) – The mappings that are applied when theTransform
is called. If alist
is passed as the parameter, then this constructs an implicit'parallel'
transform.
- __weakref__
list of weak references to the object (if defined)