diff --git a/CADETProcess/processModel/componentSystem.py b/CADETProcess/processModel/componentSystem.py index ee259f82..fd93dd88 100644 --- a/CADETProcess/processModel/componentSystem.py +++ b/CADETProcess/processModel/componentSystem.py @@ -1,5 +1,6 @@ from collections import defaultdict from functools import wraps +from typing import List, Dict as TypingDict from addict import Dict @@ -7,10 +8,8 @@ from CADETProcess.dataStructure import Structure from CADETProcess.dataStructure import String, Integer, UnsignedFloat - __all__ = ['ComponentSystem', 'Component', 'Species'] - class Species(Structure): """Species class. @@ -28,10 +27,10 @@ class Species(Structure): Density of the species. """ - name = String() - charge = Integer(default=0) - molecular_weight = UnsignedFloat() - density = UnsignedFloat() + name: String = String() + charge: Integer = Integer(default=0) + molecular_weight: UnsignedFloat = UnsignedFloat() + density: UnsignedFloat = UnsignedFloat() class Component(Structure): @@ -62,16 +61,16 @@ class Component(Structure): ComponentSystem """ - name = String() + name: String = String() def __init__( - self, - name=None, - species=None, - charge=None, - molecular_weight=None, - density=None - ): + self, + name: str | None = None, + species: str | List[str | None] = None, + charge: int | List[int | None] = None, + molecular_weight: float| List[float | None] = None, + density: List[float | None] = None + ) -> None: """ Parameters ---------- @@ -86,8 +85,8 @@ def __init__( density : list Density of component (including species). """ - self.name = name - self._species = [] + self.name: str | None = name + self._species: List[Species] = [] if species is None: self.add_species(name, charge, molecular_weight, density) @@ -106,12 +105,17 @@ def __init__( raise CADETProcessError("Could not determine number of species") @property - def species(self): + def species(self) -> List[Species]: """list: The subspecies of the component.""" return self._species @wraps(Species.__init__) - def add_species(self, species, *args, **kwargs): + def add_species( + self, + species: str| Species , + *args, + **kwargs + ) -> Species: """ Add a subspecies to the component. @@ -130,33 +134,34 @@ def add_species(self, species, *args, **kwargs): if not isinstance(species, Species): species = Species(species, *args, **kwargs) self._species.append(species) + return species @property - def n_species(self): + def n_species(self) -> int: """int: The number of subspecies in the component.""" return len(self.species) @property - def label(self): + def label(self) -> List[str]: """list of str: The names of the subspecies.""" return [spec.name for spec in self.species] @property - def charge(self): + def charge(self) -> List[int | None]: """list of int or None: The charges of the subspecies.""" return [spec.charge for spec in self.species] @property - def molecular_weight(self): + def molecular_weight(self) -> List[float | None]: """list of float or None: The molecular weights of the subspecies.""" return [spec.molecular_weight for spec in self.species] @property - def density(self): + def density(self) -> List[float | None]: """list of float or None: The density of the subspecies.""" return [spec.density for spec in self.species] - def __str__(self): + def __str__(self) -> str: """String representation of the component.""" return self.name @@ -201,41 +206,41 @@ class ComponentSystem(Structure): Component """ - name = String() + name: String = String() def __init__( - self, - components=None, - name=None, - charges=None, - molecular_weights=None, - densities=None - ): + self, + components: int | List[str | Component | None] = None, + name: str | None = None, + charges: List[int | None] = None, + molecular_weights: List[float | None] = None, + densities: List[float | None] = None + ) -> None: """Initialize the ComponentSystem object. - Parameters - ---------- - components : int, list, None - The number of components or the list of components to be added. - If None, no components are added. - name : str, None - The name of the ComponentSystem. - charges : list, None - The charges of each component. - molecular_weights : list, None - The molecular weights of each component. - densities : list, None - The densities of each component. - - Raises - ------ - CADETProcessError - If the `components` argument is neither an int nor a list. + Parameters + ---------- + components : int, list, None + The number of components or the list of components to be added. + If None, no components are added. + name : str, None + The name of the ComponentSystem. + charges : list, None + The charges of each component. + molecular_weights : list, None + The molecular weights of each component. + densities : list, None + The densities of each component. + + Raises + ------ + CADETProcessError + If the `components` argument is neither an int nor a list. """ - self.name = name - self._components = [] + self.name: str | None = name + self._components: List[Component] = [] if components is None: return @@ -264,12 +269,12 @@ def __init__( ) @property - def components(self): + def components(self) -> List[Component]: """list: List of components in the system.""" return self._components @property - def components_dict(self): + def components_dict(self) -> TypingDict[str, Component]: """dict: Components indexed by name.""" return { name: comp @@ -277,22 +282,27 @@ def components_dict(self): } @property - def n_components(self): + def n_components(self) -> int: """int: Number of components.""" return len(self.components) @property - def n_comp(self): + def n_comp(self) -> int: """int: Number of species.""" return self.n_species @property - def n_species(self): + def n_species(self) -> int: """int: Number of species.""" return sum([comp.n_species for comp in self.components]) @wraps(Component.__init__) - def add_component(self, component, *args, **kwargs): + def add_component( + self, + component: str | Component, + *args: list, + **kwargs: dict + ) -> None: """ Add a component to the system. @@ -317,7 +327,7 @@ def add_component(self, component, *args, **kwargs): self._components.append(component) - def remove_component(self, component): + def remove_component(self, component: str | Component) -> None: """Remove a component from the system. Parameters @@ -343,7 +353,7 @@ def remove_component(self, component): self._components.remove(component) @property - def indices(self): + def indices(self) -> TypingDict[str, List[int]]: """dict: List of species indices for each component name.""" indices = defaultdict(list) @@ -356,7 +366,7 @@ def indices(self): return Dict(indices) @property - def species_indices(self): + def species_indices(self) -> TypingDict[str, int]: """dict: Indices for each species.""" indices = Dict() @@ -369,7 +379,7 @@ def species_indices(self): return indices @property - def names(self): + def names(self) -> List[str]: """list: List of component names.""" names = [ comp.name if comp.name is not None else str(i) @@ -379,7 +389,7 @@ def names(self): return names @property - def species(self): + def species(self) -> List[str]: """list: List of species names.""" species = [] index = 0 @@ -395,7 +405,7 @@ def species(self): return species @property - def charges(self): + def charges(self) -> List[int | None]: """list: List of species charges.""" charges = [] for comp in self.components: @@ -404,7 +414,7 @@ def charges(self): return charges @property - def molecular_weights(self): + def molecular_weights(self) -> List[float | None]: """list: List of species molecular weights.""" molecular_weights = [] for comp in self.components: @@ -413,7 +423,7 @@ def molecular_weights(self): return molecular_weights @property - def densities(self): + def densities(self) -> List[float | None]: """list: List of species densities.""" densities = [] for comp in self.components: @@ -421,11 +431,12 @@ def densities(self): return densities - def __repr__(self): + def __repr__(self) -> str: return f'{self.__class__.__name__}({self.names})' def __iter__(self): yield from self.components - def __getitem__(self, item): + def __getitem__(self, item: int) -> Component: return self._components[item] + \ No newline at end of file