-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from portyanikhin/8-units-systems
Added the ability to configure the units system using the config file
- Loading branch information
Showing
24 changed files
with
825 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from .config import * | ||
from .enums import * | ||
from .fluids import * | ||
from .humid_air import * | ||
from .io import * | ||
|
||
__all__ = enums.__all__ + fluids.__all__ + humid_air.__all__ + io.__all__ | ||
__all__ = ( | ||
config.__all__ + enums.__all__ + fluids.__all__ + humid_air.__all__ + io.__all__ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from .pyfluids_config import * | ||
from .pyfluids_config_builder import * | ||
from .unit_converter import * | ||
from .units_system import * | ||
|
||
__all__ = ( | ||
pyfluids_config.__all__ | ||
+ pyfluids_config_builder.__all__ | ||
+ unit_converter.__all__ | ||
+ units_system.__all__ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from dataclasses import dataclass | ||
|
||
from .units_system import UnitsSystem | ||
|
||
__all__ = ["PyFluidsConfig"] | ||
|
||
|
||
@dataclass | ||
class PyFluidsConfig: | ||
"""PyFluids configuration.""" | ||
|
||
units_system: UnitsSystem = UnitsSystem.SIWithCelsiusAndPercents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from configparser import ConfigParser | ||
from os.path import abspath | ||
from pathlib import Path | ||
|
||
import tomli | ||
|
||
from .pyfluids_config import PyFluidsConfig | ||
from .singleton import Singleton | ||
from .units_system import UnitsSystem | ||
|
||
__all__ = ["PyFluidsConfigBuilder"] | ||
|
||
|
||
# noinspection PyBroadException | ||
class PyFluidsConfigBuilder(metaclass=Singleton): | ||
"""PyFluids configuration builder.""" | ||
|
||
def __init__(self): | ||
"""PyFluids configuration builder.""" | ||
self.__config: PyFluidsConfig | None = None | ||
self.__config_names: list[str] = [ | ||
"pyfluids.ini", | ||
"pyfluids.json", | ||
"pyproject.toml", | ||
"tox.ini", | ||
] | ||
|
||
@property | ||
def __current_path(self) -> Path: | ||
return Path(abspath(Path.cwd())) | ||
|
||
@property | ||
def __config_file(self) -> Path | None: | ||
for root in (self.__current_path, *self.__current_path.parents): | ||
for config_name in self.__config_names: | ||
path = root / config_name | ||
if path.is_file(): | ||
return path | ||
return None | ||
|
||
@property | ||
def __config_data(self) -> str: | ||
return self.__config_file.read_text(encoding="utf-8") | ||
|
||
def build(self) -> PyFluidsConfig: | ||
""" | ||
Build PyFluids configuration. | ||
If the configuration file is not found or an incorrect | ||
configuration is found in the files "pyproject.toml" or "tox.ini", | ||
returns the default configuration. | ||
:raises ValueError: If configuration file is invalid. | ||
""" | ||
if self.__config is not None: | ||
return self.__config | ||
if self.__config_file is None: | ||
return self.__create_default_config() | ||
if self.__config_file.suffix == ".ini": | ||
return self.__load_config_from_ini_file() | ||
if self.__config_file.suffix == ".json": | ||
return self.__load_config_from_json_file() | ||
return self.__load_config_from_toml_file() | ||
|
||
def _reset(self): | ||
self.__config = None | ||
|
||
def __create_default_config(self) -> PyFluidsConfig: | ||
self.__config = PyFluidsConfig() | ||
return self.__config | ||
|
||
def __create_config_from_dict(self, config_dict: dict) -> PyFluidsConfig: | ||
config_dict["units_system"] = UnitsSystem[config_dict["units_system"]] | ||
self.__config = PyFluidsConfig(**config_dict) | ||
return self.__config | ||
|
||
def __load_config_from_ini_file(self) -> PyFluidsConfig: | ||
try: | ||
config_parser = ConfigParser() | ||
config_parser.read(self.__config_file) | ||
return self.__create_config_from_dict(dict(config_parser.items("pyfluids"))) | ||
except Exception: | ||
if self.__config_file.name.startswith("pyfluids"): | ||
self.__raise_invalid_config_exception() | ||
return self.__create_default_config() | ||
|
||
def __load_config_from_json_file(self) -> PyFluidsConfig: | ||
try: | ||
return self.__create_config_from_dict( | ||
json.loads(self.__config_data)["pyfluids"] | ||
) | ||
except Exception: | ||
self.__raise_invalid_config_exception() | ||
|
||
def __load_config_from_toml_file(self) -> PyFluidsConfig: | ||
try: | ||
return self.__create_config_from_dict( | ||
tomli.loads(self.__config_data)["tool"]["pyfluids"] | ||
) | ||
except Exception: | ||
return self.__create_default_config() | ||
|
||
def __raise_invalid_config_exception(self): | ||
raise ValueError( | ||
"Invalid PyFluids configuration! " | ||
f"Check your configuration file: {self.__config_file}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Singleton(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) | ||
return cls._instances[cls] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from .pyfluids_config import PyFluidsConfig | ||
from .pyfluids_config_builder import PyFluidsConfigBuilder | ||
from .units_system import UnitsSystem | ||
|
||
__all__ = ["UnitConverter"] | ||
|
||
|
||
class UnitConverter: | ||
"""Unit converter.""" | ||
|
||
def __init__(self): | ||
"""Unit converter.""" | ||
self.__config: PyFluidsConfig = PyFluidsConfigBuilder().build() | ||
|
||
@property | ||
def units_system(self) -> UnitsSystem: | ||
"""Configured units system.""" | ||
return self.__config.units_system | ||
|
||
def convert_temperature_from_si(self, value: float) -> float: | ||
""" | ||
Convert temperature from SI to configured units system. | ||
:param value: Temperature value. | ||
:return: Converted value. | ||
""" | ||
if self.units_system == UnitsSystem.SI: | ||
return value | ||
return value - 273.15 | ||
|
||
def convert_temperature_to_si(self, value: float) -> float: | ||
""" | ||
Convert temperature from configured units system to SI. | ||
:param value: Temperature value. | ||
:return: Converted value. | ||
""" | ||
if self.units_system == UnitsSystem.SI: | ||
return value | ||
return value + 273.15 | ||
|
||
def convert_decimal_fraction_from_si(self, value: float) -> float: | ||
""" | ||
Convert decimal fraction from SI to configured units system. | ||
:param value: Decimal fraction value. | ||
:return: Converted value. | ||
""" | ||
if self.units_system == UnitsSystem.SIWithCelsiusAndPercents: | ||
return value * 1e2 | ||
return value | ||
|
||
def convert_decimal_fraction_to_si(self, value: float) -> float: | ||
""" | ||
Convert decimal fraction from configured units system to SI. | ||
:param value: Decimal fraction value. | ||
:return: Converted value. | ||
""" | ||
if self.units_system == UnitsSystem.SIWithCelsiusAndPercents: | ||
return value * 1e-2 | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from enum import Enum | ||
|
||
__all__ = ["UnitsSystem"] | ||
|
||
|
||
class UnitsSystem(Enum): | ||
"""List of available units systems.""" | ||
|
||
SI = "SI" | ||
SIWithCelsius = "SIWithCelsius" | ||
SIWithCelsiusAndPercents = "SIWithCelsiusAndPercents" | ||
|
||
def __repr__(self) -> str: | ||
return self.name | ||
|
||
def __str__(self) -> str: | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.