src/xstream_piflow_route.py
1"""PiFlow Route adapted to fixed result/matched/unmatched output ports."""
2from dacp_piflow_support import params, predicate_indices
3
4
5def process(ctx):
6 p = params(ctx, ['condition'])
7 source = ctx.input('source')
8 outputs = {name: ctx.records_output(name, source.schema) for name in ('result', 'matched', 'unmatched')}
9 for batch in source.records():
10 keep = predicate_indices(batch, p['condition'])
11 selected = set(keep)
12 rest = [i for i in range(batch.num_rows) if i not in selected]
13 outputs['result'].write(batch)
14 if keep:
15 outputs['matched'].write(batch.take(keep))
16 if rest:
17 outputs['unmatched'].write(batch.take(rest))
18
19
20def infer_outputs(inputs, params):
21 import pyarrow as pa
22 from xstream_operator import descriptor, result, records_schema
23 expected = {'source'}
24 if set(inputs) != expected:
25 raise ValueError('unexpected input ports')
26 outputs = {'result': {'representations': ['records'], 'formats': ['arrow'], 'certainty': 'unknown'}, 'matched': {'representations': ['records'], 'formats': ['arrow'], 'certainty': 'unknown'}, 'unmatched': {'representations': ['records'], 'formats': ['arrow'], 'certainty': 'unknown'}}
27 schema = records_schema(inputs['source']) if 'source' in inputs else None
28 if schema is not None:
29 outputs = {p: descriptor(['records'], ['arrow'], schema, certainty='known') for p in outputs}
30 return result(outputs)
31
32
33def preview_outputs(inputs, params, limits):
34 from xstream_operator import preview_process
35 return preview_process(process, inputs, params, limits)
36
37
38if __name__ == '__main__':
39 from xstream_operator import run_operator
40 run_operator(process, infer_outputs, preview_outputs=preview_outputs)