|
25 | 25 |
|
26 | 26 | from __future__ import annotations |
27 | 27 |
|
| 28 | +from typing import Generic, Type, TypeVar |
| 29 | + |
28 | 30 | from ansys.dpf.core import errors, server as server_module |
29 | 31 | from ansys.dpf.core.any import Any |
30 | | -from ansys.dpf.core.collection_base import TYPE, CollectionBase |
| 32 | +from ansys.dpf.core.collection_base import CollectionBase |
31 | 33 | from ansys.dpf.core.common import create_dpf_instance |
32 | 34 |
|
| 35 | +TYPE = TypeVar("TYPE") |
| 36 | + |
33 | 37 |
|
34 | | -class Collection(CollectionBase[TYPE]): |
| 38 | +# Explicit Generic[TYPE] helps some type checkers Collection as a generic. |
| 39 | +class Collection(CollectionBase[TYPE], Generic[TYPE]): |
35 | 40 | """Represents a collection of dpf objects organised by label spaces. |
36 | 41 |
|
37 | 42 | Parameters |
@@ -120,16 +125,39 @@ def add_entry(self, label_space, entry): |
120 | 125 | """ |
121 | 126 | return super()._add_entry(label_space, Any.new_from(entry, server=self._server)) |
122 | 127 |
|
| 128 | + @classmethod |
| 129 | + def collection_factory(cls, subtype: TYPE) -> Type[Collection[TYPE]]: |
| 130 | + """Create classes deriving from Collection at runtime for a given subtype. |
123 | 131 |
|
124 | | -def CollectionFactory(subtype, BaseClass=Collection): |
125 | | - """Create classes deriving from Collection at runtime for a given subtype.""" |
| 132 | + This factory method dynamically creates a new class that inherits from Collection |
| 133 | + and is specialized for storing entries of the specified subtype. |
126 | 134 |
|
127 | | - def __init__(self, **kwargs): |
128 | | - BaseClass.__init__(self, **kwargs) |
| 135 | + Parameters |
| 136 | + ---------- |
| 137 | + subtype : type |
| 138 | + Any recognized DPF type. For example, CustomTypeField, GenericDataContainer, |
| 139 | + StringField, Operator, etc. This type will be used as the entries_type for |
| 140 | + the new collection class. |
129 | 141 |
|
130 | | - new_class = type( |
131 | | - str(subtype.__name__) + "sCollection", |
132 | | - (BaseClass,), |
133 | | - {"__init__": __init__, "entries_type": subtype}, |
134 | | - ) |
135 | | - return new_class |
| 142 | + Returns |
| 143 | + ------- |
| 144 | + Type[Collection[TYPE]] |
| 145 | + A new class that inherits from Collection and is specialized for the given |
| 146 | + subtype. The class name will be "{subtype.__name__}sCollection". |
| 147 | +
|
| 148 | + Examples |
| 149 | + -------- |
| 150 | + >>> from ansys.dpf.core.string_field import StringField |
| 151 | + >>> from ansys.dpf.core.collection import Collection |
| 152 | + >>> string_fields_collection = Collection.collection_factory(StringField)() |
| 153 | + >>> string_fields_collection.__class__.__name__ |
| 154 | + 'StringFieldsCollection' |
| 155 | + >>> string_fields_collection.entries_type.__name__ |
| 156 | + 'StringField' |
| 157 | + """ |
| 158 | + new_class = type( |
| 159 | + str(subtype.__name__) + "sCollection", |
| 160 | + (cls,), |
| 161 | + {"entries_type": subtype}, |
| 162 | + ) |
| 163 | + return new_class |
0 commit comments