From e48be57c37f29037afcd952c32d282d682e29853 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Tue, 17 Mar 2020 10:21:19 +0000 Subject: [PATCH] RF/ENH: Wrapper outputs are now accessible as attributes in addition to dict items (where possible). Result standard output renamed from "output" to "stdout". --- fsl/wrappers/wrapperutils.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/fsl/wrappers/wrapperutils.py b/fsl/wrappers/wrapperutils.py index ac9a55c6b..cd248dd55 100644 --- a/fsl/wrappers/wrapperutils.py +++ b/fsl/wrappers/wrapperutils.py @@ -545,20 +545,44 @@ class _FileOrThing(object): items, with the argument name as key, and the output object (the "thing") as value. + Where possible (i.e. for outputs named with a valid Python + identifier), the outputs are also made accessible as attributes of + this + The decorated function's actual return value is accessible via the - :meth:`output` property. + :meth:`stdout` property. """ - def __init__(self, output): + def __init__(self, stdout): + """Create a ``_Results`` dict. + + :arg stdout: Return value of the ecorated function (typically the + standard output of the underlying command). + """ super().__init__() - self.__output = output + self.__stdout = stdout + + + def __setitem__(self, key, val): + """Add an item to the dict. The item is also added as an attribute + if possible. + """ + super().__setitem__(key, val) + + # try and add as an attribute too, + # but don't bother if the key cannot + # be used as a python identifier + try: + setattr(self, key, val) + except AttributeError: + pass @property - def output(self): + def stdout(self): """Access the return value of the decorated function. """ - return self.__output + return self.__stdout def __init__(self, -- GitLab