Skip to content

Commit

Permalink
Add type annotations
Browse files Browse the repository at this point in the history
Fix Annotations

Fix annotations and Remove unused annotations
  • Loading branch information
daklauss committed Dec 6, 2024
1 parent f7d29bd commit 22e147c
Showing 1 changed file with 79 additions and 68 deletions.
147 changes: 79 additions & 68 deletions CADETProcess/processModel/componentSystem.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from collections import defaultdict
from functools import wraps
from typing import List, Dict as TypingDict

from addict import Dict

from CADETProcess import CADETProcessError
from CADETProcess.dataStructure import Structure
from CADETProcess.dataStructure import String, Integer, UnsignedFloat


__all__ = ['ComponentSystem', 'Component', 'Species']


class Species(Structure):
"""Species class.
Expand All @@ -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):
Expand Down Expand Up @@ -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
----------
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -264,35 +269,40 @@ 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
for name, comp in zip(self.names, self.components)
}

@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.
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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()

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -413,19 +423,20 @@ 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:
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]

0 comments on commit 22e147c

Please sign in to comment.