Python Scripting
Summary
Applies a python script on input data. Additional input data nodes may be specified.
Example:
Situation | |
---|---|
Settings | |
Result | |
Project-File |
Simple Main
The simple_main method is available starting from version 6.0 of TIS. In older versions you need to provide a wrapper class. Write your code into the simple_main method.
class
SimpleMain:
# Wrapper for simple_main(params, sponge_list, types, columns).
types
=
{}
columns
=
{}
def
__init__(
self
, user_script, params, inp):
self
.user_script
=
user_script
self
.params
=
params
self
.inp
=
inp
async
def
sponge(
self
):
# Async generator that yields results from user_script.
for
res
in
self
.user_script(
self
.params,
[row async
for
row
in
self
.inp],
self
.types,
self
.columns):
yield
res
def
get_types_columns(
self
):
# Return pair of types and columns.
return
self
.types,
self
.columns
def
simple_main(params, mylist, types, columns):
#
# do something
#
return
mylist
def
main(params, inputs, outputs):
inp, out
=
inputs[
0
], outputs[
0
]
simple
=
SimpleMain(simple_main, params, inp)
return
[(simple.sponge(), out)], {
inp: simple.get_types_columns,
out: simple.get_types_columns
}
Want to learn more?
Settings
Deletes columns or ranges of columns from the data table.
Columns of input table
Troubleshooting
Problem | Frequent Cause | Solutions |
---|---|---|
... | ... |
Sample Scripts
Sample Project
Input data structure
By convention the first 2 rows define the types and captions of the input columns.
def
main(params,inputs,outputs):
columns
=
{}
types
=
{}
async
def
getcolfilter():
nonlocal columns
v
=
None
while
1
:
v
=
yield
v
i0
=
inputs[
0
]
o0
=
outputs[
0
]
return
[ (i0, getcolfilter(), o0) ], {i0: (
lambda
: (types, columns))}
Refer to columns by Caption
Use nonlocal
to refer to variables in the main method's scope in order to be available in another nested method, such as inc
in this sample.
Note: gen.__anext__()
will be replaced by anext(gen)
in Version 3.7, see https://www.python.org/dev/peps/pep-0525/#aiter-and-anext-builtins
# one input, one output
# duplicate input rows with row ids
def
main(params,inputs,outputs):
Birthday
=
None
columns
=
{}
types
=
{}
async
def
getcolfilter(i):
nonlocal Birthday
nonlocal columns
Birthday
=
columns[
'Birthday'
]
async
for
v
in
i:
yield
v
async
def
inc():
nonlocal Birthday
v
=
None
while
1
:
v
=
yield
v
# produce v, then wait for next v
v[Birthday]
+
=
datetime.timedelta(days
=
365
)
i0
=
inputs[
0
]
o0
=
outputs[
0
]
return
[ (getcolfilter(i0), inc(), o0) ], {i0: (
lambda
: (types, columns))}
Aggregate input into a list
Each yield
internally calls asend
then waits with anext
.
Prime
Every generator must be initialized with a None
, otherwise, following exception occurs:
await o.asend(v) File "user_script.py", line 18, in flush TypeError: can't send non-None value to a just-started async generator
# aggregates complete input into a list
# flushes list to output
def
main(params, inputs, outputs):
async
def
sponge(i):
mylist
=
[]
async
for
v
in
i:
mylist.append(v)
yield
mylist
async
def
flush(o):
mylist
=
yield
await o.asend(
None
)
# prime o
for
v
in
mylist:
await o.asend(v)
i0
=
inputs[
0
]
o0
=
outputs[
0
]
return
[ (sponge(i0), flush(o0)) ]
Add a new column to output
Input has a date column named "Birthday".
An output row v
is a list, so appending creates an additional column.
# input has a date column named 'Birthday'
# add a new column 'age' to output
def
main(params,inputs,outputs):
from
datetime
import
date
columns
=
{}
types
=
{}
async
def
getcolfilter():
nonlocal columns
v
=
None
while
1
:
v
=
yield
v
async
def
addage():
nonlocal columns, types
types[
len
(columns)]
=
int
columns[
'Age'
]
=
len
(columns)
v
=
None
while
1
:
v
=
yield
v
a
=
age(v[columns[
'Birthday'
]])
v.append(a)
def
age(birthday):
today
=
date.today()
age
=
(today.year
-
birthday.year
-
\
((today.month, today.day) < (birthday.month, birthday.day)))
return
age
i0
=
inputs[
0
]
o0
=
outputs[
0
]
return
[ (i0, getcolfilter(), addage(), o0) ], {i0:(
lambda
: (types,columns)), o0:(
lambda
: (types,columns))}
Related topics
- Starting to work with python for consultants