Skip to content

Commit 5be97fa

Browse files
committed
CU-8699vq0he: Improve addon access from CAT (#55)
* CU-8699vq0he: Add methods for easier addon access * CU-8699vq0he: Add some docstrings for addon-related methods
1 parent 0121ea1 commit 5be97fa

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

medcat-v2/medcat/cat.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Optional, Union, Any, overload, Literal, Iterable, Iterator
2-
from typing import cast
2+
from typing import cast, Type, TypeVar
33
import os
44
import json
55
from datetime import date
@@ -39,6 +39,9 @@
3939
logger = logging.getLogger(__name__)
4040

4141

42+
AddonType = TypeVar("AddonType", bound="AddonComponent")
43+
44+
4245
class CAT(AbstractSerialisable):
4346
"""This is a collection of serialisable model parts.
4447
"""
@@ -839,9 +842,36 @@ def __eq__(self, other: Any) -> bool:
839842
# addon (e.g MetaCAT) related stuff
840843

841844
def add_addon(self, addon: AddonComponent) -> None:
845+
"""Add the addon to the model pack an pipe.
846+
847+
Args:
848+
addon (AddonComponent): The addon to add.
849+
"""
842850
self.config.components.addons.append(addon.config)
843851
self._pipeline.add_addon(addon)
844852

853+
def get_addons(self) -> list[AddonComponent]:
854+
"""Get the list of all addons in this model pack.
855+
856+
Returns:
857+
list[AddonComponent]: The list of addons present.
858+
"""
859+
return list(self._pipeline.iter_addons())
860+
861+
def get_addons_of_type(self, addon_type: Type[AddonType]) -> list[AddonType]:
862+
"""Get a list of addons of a specific type.
863+
864+
Args:
865+
addon_type (Type[AddonType]): The type of addons to look for.
866+
867+
Returns:
868+
list[AddonType]: The list of addons of this specific type.
869+
"""
870+
return [
871+
addon for addon in self.get_addons()
872+
if isinstance(addon, addon_type)
873+
]
874+
845875

846876
class OutOfDataException(ValueError):
847877
pass

0 commit comments

Comments
 (0)