Skip to content
Open
1 change: 1 addition & 0 deletions doc/changelog.d/5117.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make ListObject a Sequence
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ of container objects: :obj:`~ansys.fluent.core.solver.flobject.Group`,
children can be accessed via ``<NamedObject>.get_object_names()``.

- The :obj:`~ansys.fluent.core.solver.flobject.ListObject` type is a container holding dynamically
created unnamed objects of
created, unnamed objects of
its specified child type (accessible via a ``child_object_type`` attribute) in a
list. Children of a ``ListObject`` object can be accessed using the index operator.
:term:`sequence`\. ``ListObject`` behaves much like a tuple though it supports :meth:`object.__setitem__`.
Comment thread
Gobot1234 marked this conversation as resolved.
Children of a ``ListObject`` object can be accessed using the index operator.
For example, ``solver_session.settings.setup.cell_zone_conditions.fluid['fluid-1'].sources.terms['mass'][2]``
refers to the third (starting from index 0) mass source entry for the fluid zone
named ``fluid-1``. The current number of child objects can be accessed with the
``get_size()`` method.
named ``fluid-1``.

.. vale Google.Spacing = YES

Expand Down
21 changes: 15 additions & 6 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from __future__ import annotations

import collections
from collections.abc import Callable
from collections.abc import Callable, Sequence
from contextlib import contextmanager, nullcontext, suppress
from enum import Enum
import fnmatch
Expand All @@ -66,6 +66,7 @@
_eval_type,
get_args,
get_origin,
overload,
)
import warnings
import weakref
Expand Down Expand Up @@ -1403,7 +1404,7 @@ def __setitem__(self, name, value):
self[name].set_state(value)


ChildTypeT = TypeVar("ChildTypeT")
ChildTypeT = TypeVar("ChildTypeT", bound="SettingsBase")


class NamedObject(SettingsBase[DictStateType], Generic[ChildTypeT]):
Expand Down Expand Up @@ -1669,7 +1670,7 @@ def _rename(obj: NamedObject | _Alias, new: str, old: str):
obj._create_child_object(new)


class ListObject(SettingsBase[ListStateType], Generic[ChildTypeT]):
class ListObject(SettingsBase[ListStateType], Sequence[ChildTypeT]):
"""A ``ListObject`` container is a container object, similar to a Python list
object. Generally, many such objects can be created.

Expand Down Expand Up @@ -1743,10 +1744,18 @@ def get_size(self) -> int:
"""
return self.flproxy.get_list_size(self.path)

def __getitem__(self, index: int) -> ChildTypeT:
@overload
def __getitem__(self, index: int) -> ChildTypeT: ...

@overload
def __getitem__(self, index: slice) -> list[ChildTypeT]: ...
Comment thread
Gobot1234 marked this conversation as resolved.

def __getitem__(self, index: int | slice) -> ChildTypeT | list[ChildTypeT]:
size = self.get_size()
if index >= size:
raise IndexError(index)
if isinstance(index, int):
if index >= size:
raise IndexError(index)

if len(self._objects) != size:
self._update_objects()
return self._objects[index]
Expand Down
13 changes: 13 additions & 0 deletions tests/test_flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,22 @@ def test_list_object():
{"il_1": None, "bl_1": None},
{"il_1": [1, 2], "bl_1": None},
]

assert r.l_1[0:1][0].il_1() is None
assert r.l_1[1:2][0].il_1() == [1, 2]

r.l_1 = [{"il_1": [3], "bl_1": [True, False]}]
assert r.l_1() == [{"il_1": [3], "bl_1": [True, False]}]

r.l_1 = [
{"il_1": [1], "bl_1": None},
{"il_1": None, "bl_1": None},
{"il_1": [2], "bl_1": None},
{"il_1": None, "bl_1": None},
{"il_1": [3], "bl_1": None},
]
assert [inner.il_1() for inner in r.l_1[::2]] == [[1], [2], [3]]


def test_list_object_set_state_with_quantity_values():
class RealWithUnits(Real):
Expand Down
Loading