Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
96736732
Commit
96736732
authored
4 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
RF: make FileOrThing+Results classes "public:, for type testing
parent
346147da
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/wrappers/wrapperutils.py
+49
-49
49 additions, 49 deletions
fsl/wrappers/wrapperutils.py
with
49 additions
and
49 deletions
fsl/wrappers/wrapperutils.py
+
49
−
49
View file @
96736732
...
@@ -403,18 +403,18 @@ def namedPositionals(func, args):
...
@@ -403,18 +403,18 @@ def namedPositionals(func, args):
LOAD
=
object
()
LOAD
=
object
()
"""
Constant used by the :class:`
_
FileOrThing` class to indicate that an output
"""
Constant used by the :class:`FileOrThing` class to indicate that an output
file should be loaded into memory and returned as a Python object.
file should be loaded into memory and returned as a Python object.
"""
"""
class
_
FileOrThing
(
object
):
class
FileOrThing
(
object
):
"""
Decorator which ensures that certain arguments which are passed into the
"""
Decorator which ensures that certain arguments which are passed into the
decorated function are always passed as file names. Both positional and
decorated function are always passed as file names. Both positional and
keyword arguments can be specified.
keyword arguments can be specified.
The ``
_
FileOrThing`` class is not intended to be used directly - see the
The ``FileOrThing`` class is not intended to be used directly - see the
:func:`fileOrImage` and :func:`fileOrArray` decorator functions for more
:func:`fileOrImage` and :func:`fileOrArray` decorator functions for more
details.
details.
...
@@ -445,7 +445,7 @@ class _FileOrThing(object):
...
@@ -445,7 +445,7 @@ class _FileOrThing(object):
**Return value**
**Return value**
Functions decorated with a ``
_
FileOrThing`` decorator will always return a
Functions decorated with a ``FileOrThing`` decorator will always return a
``dict``-like object, where the function
'
s actual return value is
``dict``-like object, where the function
'
s actual return value is
accessible via an attribute called ``stdout``. All output arguments with a
accessible via an attribute called ``stdout``. All output arguments with a
value of ``LOAD`` will be present as dictionary entries, with the keyword
value of ``LOAD`` will be present as dictionary entries, with the keyword
...
@@ -460,7 +460,7 @@ class _FileOrThing(object):
...
@@ -460,7 +460,7 @@ class _FileOrThing(object):
The above description holds in all situations, except when an argument
The above description holds in all situations, except when an argument
called ``submit`` is passed, and is set to a value which evaluates to
called ``submit`` is passed, and is set to a value which evaluates to
``True``. In this case, the ``
_
FileOrThing`` decorator will pass all
``True``. In this case, the ``FileOrThing`` decorator will pass all
arguments straight through to the decorated function, and will return its
arguments straight through to the decorated function, and will return its
return value unchanged.
return value unchanged.
...
@@ -534,27 +534,27 @@ class _FileOrThing(object):
...
@@ -534,27 +534,27 @@ class _FileOrThing(object):
**Using with other decorators**
**Using with other decorators**
``
_
FileOrThing`` decorators can be chained with other ``
_
FileOrThing``
``FileOrThing`` decorators can be chained with other ``FileOrThing``
decorators, and other decorators. When multiple ``
_
FileOrThing``
decorators, and other decorators. When multiple ``FileOrThing``
decorators are used on a single function, the outputs from each decorator
decorators are used on a single function, the outputs from each decorator
are merged together into a single dict-like object.
are merged together into a single dict-like object.
``
_
FileOrThing`` decorators can be used with any other decorators
``FileOrThing`` decorators can be used with any other decorators
**as long as** they do not manipulate the return value, and as long as
**as long as** they do not manipulate the return value, and as long as
the ``
_
FileOrThing`` decorators are adjacent to each other.
the ``FileOrThing`` decorators are adjacent to each other.
"""
"""
class
_
Results
(
dict
):
class
Results
(
dict
):
"""
A custom ``dict`` type used to return outputs from a function
"""
A custom ``dict`` type used to return outputs from a function
decorated with ``
_
FileOrThing``. All outputs are stored as dictionary
decorated with ``FileOrThing``. All outputs are stored as dictionary
items, with the argument name as key, and the output object (the
items, with the argument name as key, and the output object (the
"
thing
"
) as value.
"
thing
"
) as value.
Where possible (i.e. for outputs named with a valid Python
Where possible (i.e. for outputs named with a valid Python
identifier), the outputs are also made accessible as attributes of
identifier), the outputs are also made accessible as attributes of
the ``
_
Results`` object.
the ``Results`` object.
The decorated function
'
s actual return value is accessible via the
The decorated function
'
s actual return value is accessible via the
:meth:`stdout` property.
:meth:`stdout` property.
...
@@ -562,7 +562,7 @@ class _FileOrThing(object):
...
@@ -562,7 +562,7 @@ class _FileOrThing(object):
def
__init__
(
self
,
stdout
):
def
__init__
(
self
,
stdout
):
"""
Create a ``
_
Results`` dict.
"""
Create a ``Results`` dict.
:arg stdout: Return value of the decorated function (typically a
:arg stdout: Return value of the decorated function (typically a
tuple containing the standard output and error of the
tuple containing the standard output and error of the
...
@@ -593,7 +593,7 @@ class _FileOrThing(object):
...
@@ -593,7 +593,7 @@ class _FileOrThing(object):
removeExt
,
removeExt
,
*
args
,
*
args
,
**
kwargs
):
**
kwargs
):
"""
Initialise a ``
_
FileOrThing`` decorator.
"""
Initialise a ``FileOrThing`` decorator.
:arg func: The function to be decorated.
:arg func: The function to be decorated.
...
@@ -618,7 +618,7 @@ class _FileOrThing(object):
...
@@ -618,7 +618,7 @@ class _FileOrThing(object):
All other positional arguments are interpreted as the names of the
All other positional arguments are interpreted as the names of the
arguments to the function which will be handled by this
arguments to the function which will be handled by this
``
_
FileOrThing`` decorator. If not provided, *all* arguments passed to
``FileOrThing`` decorator. If not provided, *all* arguments passed to
the function will be handled.
the function will be handled.
...
@@ -673,8 +673,8 @@ class _FileOrThing(object):
...
@@ -673,8 +673,8 @@ class _FileOrThing(object):
'
or LOAD with submit=True!
'
)
'
or LOAD with submit=True!
'
)
return
func
(
*
args
,
**
kwargs
)
return
func
(
*
args
,
**
kwargs
)
# If this
_
FileOrThing is being called
# If this FileOrThing is being called
# by another
_
FileOrThing don't create
# by another FileOrThing don't create
# another working directory. We do this
# another working directory. We do this
# sneakily, by setting an attribute on
# sneakily, by setting an attribute on
# the wrapped function which stores the
# the wrapped function which stores the
...
@@ -734,8 +734,8 @@ class _FileOrThing(object):
...
@@ -734,8 +734,8 @@ class _FileOrThing(object):
passed to the ``prepOut`` function specified at :meth:`__init__`.
passed to the ``prepOut`` function specified at :meth:`__init__`.
All other arguments are passed through the ``prepIn`` function.
All other arguments are passed through the ``prepIn`` function.
:arg parent: ``True`` if this ``
_
FileOrThing`` is the first in a
:arg parent: ``True`` if this ``FileOrThing`` is the first in a
chain of ``
_
FileOrThing`` decorators.
chain of ``FileOrThing`` decorators.
:arg workdir: Directory in which all temporary files should be stored.
:arg workdir: Directory in which all temporary files should be stored.
...
@@ -779,7 +779,7 @@ class _FileOrThing(object):
...
@@ -779,7 +779,7 @@ class _FileOrThing(object):
# Prefixed outputs are only
# Prefixed outputs are only
# managed by the parent
# managed by the parent
#
_
FileOrthing in a chain of
# FileOrthing in a chain of
# FoT decorators.
# FoT decorators.
if
not
parent
:
if
not
parent
:
prefix
=
None
prefix
=
None
...
@@ -894,11 +894,11 @@ class _FileOrThing(object):
...
@@ -894,11 +894,11 @@ class _FileOrThing(object):
def
__generateResult
(
def
__generateResult
(
self
,
workdir
,
result
,
outprefix
,
outfiles
,
prefixes
):
self
,
workdir
,
result
,
outprefix
,
outfiles
,
prefixes
):
"""
Loads function outputs and returns a :class:`
_
Results` object.
"""
Loads function outputs and returns a :class:`Results` object.
Called by :meth:`__call__` after the decorated function has been
Called by :meth:`__call__` after the decorated function has been
called. Figures out what files should be loaded, and loads them into
called. Figures out what files should be loaded, and loads them into
a ``
_
Results`` object.
a ``Results`` object.
:arg workdir: Directory which contains the function outputs.
:arg workdir: Directory which contains the function outputs.
:arg result: Function return value.
:arg result: Function return value.
...
@@ -909,16 +909,16 @@ class _FileOrThing(object):
...
@@ -909,16 +909,16 @@ class _FileOrThing(object):
:arg prefixes: Dictionary containing output-prefix patterns to be
:arg prefixes: Dictionary containing output-prefix patterns to be
loaded (see :meth:`__prepareArgs`).
loaded (see :meth:`__prepareArgs`).
:returns: A ``
_
Results`` object containing all loaded outputs.
:returns: A ``Results`` object containing all loaded outputs.
"""
"""
# make a
_
Results object to store
# make a Results object to store
# the output. If we are decorating
# the output. If we are decorating
# another
_
FileOrThing, the
# another FileOrThing, the
# results will get merged together
# results will get merged together
# into a single
_
Results dict.
# into a single Results dict.
if
not
isinstance
(
result
,
_
FileOrThing
.
_
Results
):
if
not
isinstance
(
result
,
FileOrThing
.
Results
):
result
=
_
FileOrThing
.
_
Results
(
result
)
result
=
FileOrThing
.
Results
(
result
)
# Load the LOADed outputs
# Load the LOADed outputs
for
oname
,
ofile
in
outfiles
.
items
():
for
oname
,
ofile
in
outfiles
.
items
():
...
@@ -1039,13 +1039,13 @@ def fileOrImage(*args, **kwargs):
...
@@ -1039,13 +1039,13 @@ def fileOrImage(*args, **kwargs):
raise
RuntimeError
(
'
Cannot handle type: {}
'
.
format
(
intypes
))
raise
RuntimeError
(
'
Cannot handle type: {}
'
.
format
(
intypes
))
def
decorator
(
func
):
def
decorator
(
func
):
fot
=
_
FileOrThing
(
func
,
fot
=
FileOrThing
(
func
,
prepIn
,
prepIn
,
prepOut
,
prepOut
,
load
,
load
,
fslimage
.
removeExt
,
fslimage
.
removeExt
,
*
args
,
*
args
,
**
kwargs
)
**
kwargs
)
def
wrapper
(
*
args
,
**
kwargs
):
def
wrapper
(
*
args
,
**
kwargs
):
result
=
fot
(
*
args
,
**
kwargs
)
result
=
fot
(
*
args
,
**
kwargs
)
...
@@ -1081,13 +1081,13 @@ def fileOrArray(*args, **kwargs):
...
@@ -1081,13 +1081,13 @@ def fileOrArray(*args, **kwargs):
except
Exception
:
return
None
except
Exception
:
return
None
def
decorator
(
func
):
def
decorator
(
func
):
fot
=
_
FileOrThing
(
func
,
fot
=
FileOrThing
(
func
,
prepIn
,
prepIn
,
prepOut
,
prepOut
,
load
,
load
,
fslpath
.
removeExt
,
fslpath
.
removeExt
,
*
args
,
*
args
,
**
kwargs
)
**
kwargs
)
def
wrapper
(
*
args
,
**
kwargs
):
def
wrapper
(
*
args
,
**
kwargs
):
return
fot
(
*
args
,
**
kwargs
)
return
fot
(
*
args
,
**
kwargs
)
...
@@ -1148,13 +1148,13 @@ def fileOrText(*args, **kwargs):
...
@@ -1148,13 +1148,13 @@ def fileOrText(*args, **kwargs):
except
Exception
:
return
None
except
Exception
:
return
None
def
decorator
(
func
):
def
decorator
(
func
):
fot
=
_
FileOrThing
(
func
,
fot
=
FileOrThing
(
func
,
prepIn
,
prepIn
,
prepOut
,
prepOut
,
load
,
load
,
fslpath
.
removeExt
,
fslpath
.
removeExt
,
*
args
,
*
args
,
**
kwargs
)
**
kwargs
)
def
wrapper
(
*
args
,
**
kwargs
):
def
wrapper
(
*
args
,
**
kwargs
):
return
fot
(
*
args
,
**
kwargs
)
return
fot
(
*
args
,
**
kwargs
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment