Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions erfa/core.py.templ
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ STATUS_CODES['{{ func.pyname }}'] = {{ stat.to_python() }}
# For ufuncs with more than one output, define a namedtuple so one
# can access the outputs by name.
#}
{%- if func.args_by_inout('inout|out|ret')|length > 1 %}
{%- if func.result_tuple %}


{{ func.pyname|capitalize }}Result = namedtuple('{{ func.pyname|capitalize }}Result', "{{ func.args_by_inout('inout|out|ret')|map(attribute='name')|join(', ') }}")
{{ func.result_tuple.define() }}
{%- endif %}


Expand All @@ -141,8 +141,8 @@ def {{ func.pyname }}({{ func.args_by_inout('in|inout')|map(attribute='name')|jo

Returns
-------
{%- if func.args_by_inout('inout|out|ret')|length > 1 %}
A ``{{ func.pyname|capitalize }}Result`` namedtuple with the following attributes:
{%- if func.result_tuple %}
A ``{{ func.result_tuple.name }}`` namedtuple with the following attributes:
{%- endif %}
{%- for arg in func.args_by_inout('inout|out|ret') %}
{{ arg.name }} : {{ arg.ctype }} array
Expand Down Expand Up @@ -181,10 +181,10 @@ def {{ func.pyname }}({{ func.args_by_inout('in|inout')|map(attribute='name')|jo
{#-
# Return the output arguments (including the inplace ones)
#}
{%- if func.args_by_inout('inout|out|ret')|length == 1 %}
return {{ func.args_by_inout('inout|out|ret')[0].name }}
{%- if func.result_tuple %}
return {{ func.result_tuple.create() }}
{%- else %}
return {{ func.pyname|capitalize }}Result({{ func.args_by_inout('inout|out|ret')|map(attribute='name')|join(', ') }})
return {{ func.args_by_inout('inout|out|ret')[0].name }}
{%- endif %}

{%- endfor %}
Expand Down
19 changes: 19 additions & 0 deletions erfa_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import functools
import re
from collections.abc import Iterable
from pathlib import Path
from typing import Final, final

Expand Down Expand Up @@ -233,6 +234,18 @@ def __init__(self, ctype: str) -> None:
super().__init__(ctype)


class ResultTuple:
def __init__(self, func_name: str, args: Iterable[Argument | Return]) -> None:
self.name: Final = f"{func_name.capitalize()}Result"
self.arg_names: Final = ", ".join(arg.name for arg in args)

def create(self) -> str:
return f"{self.name}({self.arg_names})"

def define(self) -> str:
return f'{self.name} = namedtuple({self.name!r}, "{self.arg_names}")'


class Function:
"""
A class representing a C function.
Expand Down Expand Up @@ -267,6 +280,12 @@ def __init__(self, name: str, source_path: Path) -> None:
else Return(ret)
)

self.result_tuple: Final = (
ResultTuple(self.pyname, py_return)
if len(py_return := self.args_by_inout("inout|out|ret")) > 1
else None
)

def args_by_inout(self, inout_filter):
"""
Gives all of the arguments and/or returned values, depending on whether
Expand Down
Loading