[Tutor] Implicit passing of argument select functions being called
Peter Otten
__peter__ at web.de
Mon Jul 18 02:49:21 EDT 2022
On 14/07/2022 09:56, Αλέξανδρος Ρόδης wrote:
> I've finally figured ou a solution, I'll leave it here in case it helps
> someone else, using inspect.signature
>
>
> The actuall functions would look like this:
> `
> dfilter = simple_filter(start_col = 6,)
> make_pipeline(
> load_dataset(fpath="some/path.xlsx",
> header=[0,1],
> apply_internal_standard(target_col = "EtD5"),
> export_to_sql(fname = "some/name"),
> ,
> data _filter = dfilter
> )
Whatever works ;) I think I would instead ensure that all
transformations have the same signature. functools.partial() could be
helpful to implement this. Simple example:
>>> def add(items, value):
return [item + value for item in items]
>>> def set_value(items, value, predicate):
return [value if predicate(item) else item for item in items]
>>> def transform(items, *transformations):
for trafo in transformations:
items = trafo(items)
return items
>>> from functools import partial
>>> transform(
[-3, 7, 5],
# add 5 to each item
partial(add, value=5),
# set items > 10 to 0
partial(set_value, value=0, predicate=lambda x: x > 10)
)
[2, 0, 10]
More information about the Tutor
mailing list