Skip to content

Instances

instances

DInstances

Bases: ProtoTInstances[float]

Holder for instances.

Allows retrieval by name or index

Source code in kfactory/instances.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class DInstances(ProtoTInstances[float]):
    """Holder for instances.

    Allows retrieval by name or index
    """

    def __iter__(self) -> Iterator[DInstance]:
        """Get instance iterator."""
        yield from (
            DInstance(kcl=self._tkcell.kcl, instance=inst) for inst in self._insts
        )

    def __getitem__(self, key: str | int) -> DInstance:
        """Retrieve instance by index or by name."""
        if isinstance(key, int):
            return DInstance(kcl=self._tkcell.kcl, instance=list(self._insts)[key])
        return DInstance(kcl=self._tkcell.kcl, instance=self._get_inst(key))

__getitem__

__getitem__(key: str | int) -> DInstance

Retrieve instance by index or by name.

Source code in kfactory/instances.py
161
162
163
164
165
def __getitem__(self, key: str | int) -> DInstance:
    """Retrieve instance by index or by name."""
    if isinstance(key, int):
        return DInstance(kcl=self._tkcell.kcl, instance=list(self._insts)[key])
    return DInstance(kcl=self._tkcell.kcl, instance=self._get_inst(key))

__iter__

__iter__() -> Iterator[DInstance]

Get instance iterator.

Source code in kfactory/instances.py
155
156
157
158
159
def __iter__(self) -> Iterator[DInstance]:
    """Get instance iterator."""
    yield from (
        DInstance(kcl=self._tkcell.kcl, instance=inst) for inst in self._insts
    )

Instances

Bases: ProtoTInstances[int]

Holder for instances.

Allows retrieval by name or index

Source code in kfactory/instances.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class Instances(ProtoTInstances[int]):
    """Holder for instances.

    Allows retrieval by name or index
    """

    def __iter__(self) -> Iterator[Instance]:
        """Get instance iterator."""
        yield from (
            Instance(kcl=self._tkcell.kcl, instance=inst) for inst in self._insts
        )

    def __getitem__(self, key: str | int) -> Instance:
        """Retrieve instance by index or by name."""
        if isinstance(key, int):
            return Instance(kcl=self._tkcell.kcl, instance=list(self._insts)[key])
        return Instance(kcl=self._tkcell.kcl, instance=self._get_inst(key))

__getitem__

__getitem__(key: str | int) -> Instance

Retrieve instance by index or by name.

Source code in kfactory/instances.py
142
143
144
145
146
def __getitem__(self, key: str | int) -> Instance:
    """Retrieve instance by index or by name."""
    if isinstance(key, int):
        return Instance(kcl=self._tkcell.kcl, instance=list(self._insts)[key])
    return Instance(kcl=self._tkcell.kcl, instance=self._get_inst(key))

__iter__

__iter__() -> Iterator[Instance]

Get instance iterator.

Source code in kfactory/instances.py
136
137
138
139
140
def __iter__(self) -> Iterator[Instance]:
    """Get instance iterator."""
    yield from (
        Instance(kcl=self._tkcell.kcl, instance=inst) for inst in self._insts
    )

ProtoTInstances

Bases: ProtoInstances[T, ProtoTInstance[T]], ABC

Source code in kfactory/instances.py
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class ProtoTInstances[T: (int, float)](ProtoInstances[T, ProtoTInstance[T]], ABC):
    _tkcell: TKCell

    def __init__(self, cell: TKCell) -> None:
        """Constructor."""
        self._tkcell = cell

    @abstractmethod
    def __iter__(self) -> Iterator[ProtoTInstance[T]]: ...

    def __len__(self) -> int:
        """Length of the instances."""
        return self._tkcell.kdb_cell.child_instances()

    @property
    def _insts(self) -> Iterator[kdb.Instance]:
        yield from self._tkcell.kdb_cell.each_inst()

    def _get_inst(self, item: kdb.Instance | str) -> kdb.Instance:
        try:
            if isinstance(item, kdb.Instance):
                return next(filter(lambda inst: inst == item, self._insts))
            return next(
                filter(lambda inst: inst.property(PROPID.NAME) == item, self._insts)
            )
        except StopIteration as e:
            raise ValueError(f"Instance {item} not found in {self._tkcell}") from e

    def __delitem__(self, item: ProtoTInstance[Any] | int) -> None:
        if isinstance(item, int):
            list(self._insts)[item].delete()
        else:
            self._get_inst(item.instance).delete()

    def __contains__(self, key: str | int | ProtoTInstance[Any]) -> bool:
        try:
            if isinstance(key, ProtoTInstance):
                self._get_inst(key.instance)
                return True
            if isinstance(key, str):
                self._get_inst(key)
                return True
            return key < len(self)
        except ValueError:
            return False

    @abstractmethod
    def __getitem__(self, key: str | int) -> ProtoTInstance[T]: ...

    def clear(self) -> None:
        for inst in self._insts:
            inst.delete()

    def append(self, inst: ProtoTInstance[Any]) -> None:
        """Append a new instance."""
        self._tkcell.kdb_cell.insert(inst.instance)

    def remove(self, inst: ProtoTInstance[Any]) -> None:
        inst.instance.delete()

    def to_itype(self) -> Instances:
        return Instances(cell=self._tkcell)

    def to_dtype(self) -> DInstances:
        return DInstances(cell=self._tkcell)

__init__

__init__(cell: TKCell) -> None

Constructor.

Source code in kfactory/instances.py
66
67
68
def __init__(self, cell: TKCell) -> None:
    """Constructor."""
    self._tkcell = cell

__len__

__len__() -> int

Length of the instances.

Source code in kfactory/instances.py
73
74
75
def __len__(self) -> int:
    """Length of the instances."""
    return self._tkcell.kdb_cell.child_instances()

append

append(inst: ProtoTInstance[Any]) -> None

Append a new instance.

Source code in kfactory/instances.py
116
117
118
def append(self, inst: ProtoTInstance[Any]) -> None:
    """Append a new instance."""
    self._tkcell.kdb_cell.insert(inst.instance)

VInstances

Bases: ProtoInstances[float, VInstance]

Holder for VInstances.

Allows retrieval by name or index

Source code in kfactory/instances.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
class VInstances(ProtoInstances[float, VInstance]):
    """Holder for VInstances.

    Allows retrieval by name or index
    """

    _vinsts: list[VInstance]

    def __init__(self, vinsts: list[VInstance] | None = None) -> None:
        self._vinsts = vinsts or []

    def __iter__(self) -> Iterator[VInstance]:
        """Get instance iterator."""
        yield from self._vinsts

    def __len__(self) -> int:
        """Get the number of instances."""
        return len(self._vinsts)

    def __delitem__(self, item: VInstance | int) -> None:
        """Delete an instance by index or instance."""
        if isinstance(item, int):
            del self._vinsts[item]
        else:
            self._vinsts.remove(item)

    def __getitem__(self, key: str | int) -> VInstance:
        """Retrieve instance by index or by name."""
        if isinstance(key, int):
            return self._vinsts[key]
        for inst in self._vinsts:
            if inst.name == key:
                return inst
        raise KeyError(f"No instance found with name: {key}")

    def __contains__(self, key: str | int | VInstance) -> bool:
        if isinstance(key, VInstance):
            return key in self._vinsts
        if isinstance(key, int):
            return key < len(self)
        return any(inst.name == key for inst in self._vinsts)

    def clear(self) -> None:
        """Clear all instances."""
        self._vinsts.clear()

    def append(self, inst: VInstance) -> None:
        """Append a new instance."""
        self._vinsts.append(inst)

    def remove(self, inst: VInstance) -> None:
        """Remove an instance."""
        self._vinsts.remove(inst)

    def dup(self) -> VInstances:
        """Copy the instances."""
        return VInstances([vinst.dup() for vinst in self._vinsts])

    def copy(self) -> VInstances:
        """Copy the instances."""
        return self.dup()

__delitem__

__delitem__(item: VInstance | int) -> None

Delete an instance by index or instance.

Source code in kfactory/instances.py
187
188
189
190
191
192
def __delitem__(self, item: VInstance | int) -> None:
    """Delete an instance by index or instance."""
    if isinstance(item, int):
        del self._vinsts[item]
    else:
        self._vinsts.remove(item)

__getitem__

__getitem__(key: str | int) -> VInstance

Retrieve instance by index or by name.

Source code in kfactory/instances.py
194
195
196
197
198
199
200
201
def __getitem__(self, key: str | int) -> VInstance:
    """Retrieve instance by index or by name."""
    if isinstance(key, int):
        return self._vinsts[key]
    for inst in self._vinsts:
        if inst.name == key:
            return inst
    raise KeyError(f"No instance found with name: {key}")

__iter__

__iter__() -> Iterator[VInstance]

Get instance iterator.

Source code in kfactory/instances.py
179
180
181
def __iter__(self) -> Iterator[VInstance]:
    """Get instance iterator."""
    yield from self._vinsts

__len__

__len__() -> int

Get the number of instances.

Source code in kfactory/instances.py
183
184
185
def __len__(self) -> int:
    """Get the number of instances."""
    return len(self._vinsts)

append

append(inst: VInstance) -> None

Append a new instance.

Source code in kfactory/instances.py
214
215
216
def append(self, inst: VInstance) -> None:
    """Append a new instance."""
    self._vinsts.append(inst)

clear

clear() -> None

Clear all instances.

Source code in kfactory/instances.py
210
211
212
def clear(self) -> None:
    """Clear all instances."""
    self._vinsts.clear()

copy

copy() -> VInstances

Copy the instances.

Source code in kfactory/instances.py
226
227
228
def copy(self) -> VInstances:
    """Copy the instances."""
    return self.dup()

dup

dup() -> VInstances

Copy the instances.

Source code in kfactory/instances.py
222
223
224
def dup(self) -> VInstances:
    """Copy the instances."""
    return VInstances([vinst.dup() for vinst in self._vinsts])

remove

remove(inst: VInstance) -> None

Remove an instance.

Source code in kfactory/instances.py
218
219
220
def remove(self, inst: VInstance) -> None:
    """Remove an instance."""
    self._vinsts.remove(inst)