Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
10 changes: 6 additions & 4 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ def to_python_keys(cls, value):
query_names = []
_child_aliases = {}

def _create_child_object(self, cname: str):
def _create_child_object(self, cname: str, **kwargs: Any):
ret = self._objects.get(cname)
if not ret:
cls = self.__class__.child_object_type
Expand All @@ -1399,6 +1399,7 @@ def _create_child_object(self, cname: str):
"rename",
types.MethodType(lambda obj, name: _rename(self, name, cname), ret),
)
ret.set_state(kwargs)
return ret

def _update_objects(self):
Expand Down Expand Up @@ -1451,7 +1452,7 @@ def get_object_names(self):
obj_names_list = obj_names if isinstance(obj_names, list) else list(obj_names)
return obj_names_list

def __getitem__(self, name: str) -> ChildTypeT:
def __getitem__(self, name: str, **kwargs: Any) -> ChildTypeT:
Comment thread
Gobot1234 marked this conversation as resolved.
Comment thread
Gobot1234 marked this conversation as resolved.
if name not in self.get_object_names():
if self.flproxy.has_wildcard(name):
child_cls = self.__class__.child_object_type
Expand All @@ -1473,7 +1474,7 @@ def __getitem__(self, name: str) -> ChildTypeT:

obj = self._objects.get(name)
if not obj:
obj = self._create_child_object(name)
obj = self._create_child_object(name, **kwargs)
return obj

def get(self, name: str) -> ChildTypeT:
Expand Down Expand Up @@ -1843,7 +1844,8 @@ def execute_command(self, *args, **kwds):
and isinstance(self._parent, NamedObject)
and ret in self._parent
):
Comment thread
Gobot1234 marked this conversation as resolved.
return self._parent[ret]
nameless_kwargs = {k: v for k, v in kwds.items() if k != "name"}
return self._parent.__getitem__(ret, **nameless_kwargs)
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
return_t = getattr(self, "return_type", None)
if return_t:
base_t = _baseTypes.get(return_t)
Expand Down
69 changes: 51 additions & 18 deletions src/ansys/fluent/core/solver/settings_builtin_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

"""Base classes for builtin setting classes."""

from typing import Protocol, runtime_checkable
from typing import Any, Protocol, runtime_checkable

from typing_extensions import Self

from ansys.fluent.core.solver.flobject import (
Comment on lines +27 to 30
InactiveObjectError,
Expand Down Expand Up @@ -82,19 +84,19 @@ def _get_settings_obj(settings_root, builtin_settings_obj):
return obj


def _initialize_settings(instance, defaults: dict, settings_source=None, **kwargs):
active_session = _get_active_session()
instance.__dict__.update(defaults | kwargs)
if settings_source is not None:
instance.settings_source = settings_source
elif active_session:
instance.settings_source = active_session

class _SettingsInitializerMixin:
def __init__(self, defaults: dict, settings_source: SettingsBase | Solver | None = None, **kwargs: Any):
active_session = _get_active_session()
self.__dict__.update(defaults | kwargs)
if settings_source is not None:
self.settings_source = settings_source
elif active_session:
self.settings_source = active_session

Comment on lines +107 to +113
class _SingletonSetting:
class _SingletonSetting(_SettingsInitializerMixin):
# Covers groups, named-object containers and commands.
def __init__(self, settings_source: SettingsBase | Solver | None = None, **kwargs):
_initialize_settings(self, {"settings_source": None}, settings_source, **kwargs)
super().__init__({"settings_source": None}, settings_source, **kwargs)

def __setattr__(self, name, value):
if name == "settings_source":
Expand All @@ -106,14 +108,25 @@ def __setattr__(self, name, value):
else:
super().__setattr__(name, value)

@classmethod
def get(cls, settings_source: SettingsBase | Solver | None = None, /, *, name: str) -> Self:
"""Get and return the singleton instance of this object in Fluent.

Parameters
----------
settings_source
Something with a .settings attribute. If omitted the active session assumed from the :func:`using` context manager.
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
name
Name of the object to get, if applicable, can be a wildcard pattern.
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
"""
return cls(settings_source=settings_source, name=name)


class _NonCreatableNamedObjectSetting:
class _NonCreatableNamedObjectSetting(_SettingsInitializerMixin):
def __init__(
self, name: str, settings_source: SettingsBase | Solver | None = None, **kwargs
):
_initialize_settings(
self, {"settings_source": None, "name": name}, settings_source, **kwargs
)
super().__init__({"settings_source": None, "name": name}, settings_source, **kwargs)

def __setattr__(self, name, value):
if name == "settings_source":
Expand All @@ -127,7 +140,7 @@ def __setattr__(self, name, value):
super().__setattr__(name, value)


class _CreatableNamedObjectSetting:
class _CreatableNamedObjectSetting(_SettingsInitializerMixin):
def __init__(
self,
settings_source: SettingsBase | Solver | None = None,
Expand All @@ -137,8 +150,7 @@ def __init__(
):
if name and new_instance_name:
raise ValueError("Cannot specify both name and new_instance_name.")
_initialize_settings(
self,
super().__init__(
{
"settings_source": None,
"name": name,
Expand All @@ -148,6 +160,27 @@ def __init__(
**kwargs,
)

@classmethod
def create(cls, settings_source: SettingsBase | Solver | None = None, /, name: str | None = None, **kwargs: Any) -> Self:
"""Create and return an instance of this object in Fluent.

Parameters
----------
settings_source
Something with a .settings attribute. If omitted the active session assumed from the :func:`using` context manager.
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
name
Name of the new object to create. If omitted, a default name will be assigned by Fluent.
**kwargs
Additional attributes to set on the created object. This only works for direct value assignments, not for nested objects.
Comment thread
Gobot1234 marked this conversation as resolved.
"""
self = cls(
settings_source=settings_source,
new_instance_name=name,
)
self.set_state(kwargs)

return self

def __setattr__(self, name, value):
if name == "settings_source":
settings_root = _get_settings_root(value)
Expand Down
Loading