From 4f79e91ab7de16d774852586f37cdfe83945e5a0 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Wed, 13 Sep 2023 15:45:07 +0200 Subject: [PATCH] ENH: more versatile find_func for table export --- src/caosadvancedtools/table_export.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/caosadvancedtools/table_export.py b/src/caosadvancedtools/table_export.py index 49a795a5..63d16a41 100644 --- a/src/caosadvancedtools/table_export.py +++ b/src/caosadvancedtools/table_export.py @@ -27,6 +27,7 @@ them for an export as a table, e.g., for the export to metadata repositories. """ +from inspect import signature import json import logging @@ -163,12 +164,8 @@ class BaseTableExporter(object): else: self._append_missing(e, d) elif FIND_FUNCTION in d: - if callable(d[FIND_FUNCTION]): - find_fun = d[FIND_FUNCTION] - else: - find_fun = getattr(self, d[FIND_FUNCTION]) try: - self.info[e] = find_fun() + self.info[e] = self._call_find_function(d[FIND_FUNCTION], e); except Exception as exc: self._append_missing(e, d) logger.debug(exc) @@ -203,6 +200,22 @@ class BaseTableExporter(object): else: logger.error(errmssg) + def _call_find_function(self, find_function, e): + account_for_self = 0 + if callable(d[FIND_FUNCTION]): + find_fun = d[FIND_FUNCTION] + else: + find_fun = getattr(self, d[FIND_FUNCTION]) + account_for_self = 1 + + sig = signature(find_fun) + params = sig.parameters + if len(params) > (account_for_self + 1): + return find_fun(self.record, e) + elif len(params) > account_for_self: + return find_fun(self.record) + return find_fun() + def prepare_csv_export(self, delimiter=',', print_header=False, skip_empty_optionals=False): """Return the values in self.info as a single-line string, separated -- GitLab