From 96f6499cff907cfae3e630fa3773a6d127d24a54 Mon Sep 17 00:00:00 2001 From: daklauss Date: Fri, 6 Dec 2024 09:23:40 +0100 Subject: [PATCH] Add type annotations Fix Annotations Fix annotations and Remove unused annotations --- CADETProcess/processModel/componentSystem.py | 193 ++++++++++--------- 1 file changed, 102 insertions(+), 91 deletions(-) diff --git a/CADETProcess/processModel/componentSystem.py b/CADETProcess/processModel/componentSystem.py index ee259f82..28d69335 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 NoReturn 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): @@ -41,7 +40,7 @@ class Component(Structure): Attributes ---------- - name : String + name : str | None Name of the component. species : list List of Subspecies. @@ -49,11 +48,11 @@ class Component(Structure): Number of Subspecies. label : list Name of component (including species). - charge : list + charge : int | list[int | None] Charge of component (including species). - molecular_weight : list + molecular_weight : float | list[float | None] Molecular weight of component (including species). - density : list + density : float | list[float | None] Density of component (including species). See Also @@ -62,32 +61,32 @@ class Component(Structure): ComponentSystem """ - name = String() + name: String = String() def __init__( self, - name=None, - species=None, - charge=None, - molecular_weight=None, - density=None - ): + name: str | None = None, + species: str | list[str | None] = None, + charge: int | list[int | None] = None, + molecular_weight: float| list[float | None] = None, + density: float | list[float | None] = None + ) -> NoReturn: """ Parameters ---------- - name : str, optional + name : str | None Name of the component. - species : str or list of str, optional + species : str | list[str | None] Names of the subspecies. - charge : int or list of int or None, optional + charge : int | list [int | None] Charges of the subspecies. - molecular_weight : float or list of float or None, optional + molecular_weight : float | list[float | None] Molecular weights of the subspecies. - density : list + density : float | list[float | None] 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): - """list of str: The names of the subspecies.""" + def label(self) -> list[str]: + """list[str]: The names of the subspecies.""" return [spec.name for spec in self.species] @property - def charge(self): - """list of int or None: The charges of the subspecies.""" + def charge(self) -> list[int | None]: + """list[int | None]: The charges of the subspecies.""" return [spec.charge for spec in self.species] @property - def molecular_weight(self): - """list of float or None: The molecular weights of the subspecies.""" + def molecular_weight(self) -> list[float | None]: + """list[float | None]: The molecular weights of the subspecies.""" return [spec.molecular_weight for spec in self.species] @property - def density(self): - """list of float or None: The density of the subspecies.""" + def density(self) -> list[float | None]: + """list[float | 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 @@ -174,7 +179,7 @@ class ComponentSystem(Structure): ---------- name : String Name of the component system. - components : list + components : list[Component] List of individual components. n_species : int Number of Subspecies. @@ -182,17 +187,17 @@ class ComponentSystem(Structure): Number of all component species. n_components : int Number of components. - indices : dict + indices : dict[str, list[int]] Component indices. - names : list + names : list[str] Names of all components. - species : list + species : list[str] Names of all component species. - charges : list + charges : list[int | None] Charges of all components species. - molecular_weights : list + molecular_weights : list[float | None] Molecular weights of all component species. - densities : list + densities : list[float | None] Densities of all component species. See Also @@ -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 - ): + 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[str | Component | 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[int | None] + The charges of each component. + molecular_weights : list[float | None] + The molecular weights of each component. + densities : list[float | 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) -> dict[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 + ) -> NoReturn: """ 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) -> NoReturn: """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) -> dict[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) -> dict[str, int]: """dict: Indices for each species.""" indices = Dict() @@ -369,8 +379,8 @@ def species_indices(self): return indices @property - def names(self): - """list: List of component names.""" + def names(self) -> list[str]: + """list[str]: List of component names.""" names = [ comp.name if comp.name is not None else str(i) for i, comp in enumerate(self.components) @@ -379,8 +389,8 @@ def names(self): return names @property - def species(self): - """list: List of species names.""" + def species(self) -> list[str]: + """list[str]: List of species names.""" species = [] index = 0 for comp in self.components: @@ -395,8 +405,8 @@ def species(self): return species @property - def charges(self): - """list: List of species charges.""" + def charges(self) -> list[int | None]: + """list[int | None]: List of species charges.""" charges = [] for comp in self.components: charges += comp.charge @@ -404,8 +414,8 @@ def charges(self): return charges @property - def molecular_weights(self): - """list: List of species molecular weights.""" + def molecular_weights(self) -> list[float | None]: + """list[float | None]: List of species molecular weights.""" molecular_weights = [] for comp in self.components: molecular_weights += comp.molecular_weight @@ -413,19 +423,20 @@ def molecular_weights(self): return molecular_weights @property - def densities(self): - """list: List of species densities.""" + def densities(self) -> list[float | None]: + """list[float | None]: List of species densities.""" densities = [] for comp in self.components: densities += comp.density 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