From 2baa718d807379175a41b5577dee81452c706d75 Mon Sep 17 00:00:00 2001 From: Chi-Huan Nguyen Date: Mon, 25 Aug 2025 16:49:30 +0200 Subject: [PATCH] Formatting --- pydase_service_base/database/__init__.py | 9 +++- pydase_service_base/database/config.py | 1 + .../database/influxdbv3_session.py | 45 ++++++++++++------- tests/test_create_config.py | 1 - tests/test_influxdbv3_session.py | 2 - 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pydase_service_base/database/__init__.py b/pydase_service_base/database/__init__.py index 36ac921..e9bbf61 100644 --- a/pydase_service_base/database/__init__.py +++ b/pydase_service_base/database/__init__.py @@ -35,6 +35,7 @@ else: "InfluxDBv1Session requires the 'influxdbv1' extra. " "Please refer to https://gitlab.phys.ethz.ch/tiqi-projects/qchub/icon-services/pydase_service_base." ) + try: import influxdb_client_3 # type: ignore # noqa @@ -47,6 +48,7 @@ else: "InfluxDBv3Session requires the 'influxdbv3' extra. " "Please refer to https://gitlab.phys.ethz.ch/tiqi-projects/qchub/icon-services/pydase_service_base." ) + try: import sqlmodel # noqa @@ -61,4 +63,9 @@ else: ) -__all__ = ["InfluxDBSession", "InfluxDBv1Session", "InfluxDBv3Session", "PostgresDatabaseSession"] +__all__ = [ + "InfluxDBSession", + "InfluxDBv1Session", + "InfluxDBv3Session", + "PostgresDatabaseSession", +] diff --git a/pydase_service_base/database/config.py b/pydase_service_base/database/config.py index 072d882..c19374c 100644 --- a/pydase_service_base/database/config.py +++ b/pydase_service_base/database/config.py @@ -43,6 +43,7 @@ class InfluxDBv1Config(BaseConfig): # type: ignore verify_ssl: bool = True headers: dict[str, str] = {} # noqa: RUF012 + class InfluxDBv3Config(BaseConfig): # type: ignore url: str org: str diff --git a/pydase_service_base/database/influxdbv3_session.py b/pydase_service_base/database/influxdbv3_session.py index 9aded39..42d16f5 100644 --- a/pydase_service_base/database/influxdbv3_session.py +++ b/pydase_service_base/database/influxdbv3_session.py @@ -1,4 +1,5 @@ """Module for InfluxDB v3 session management.""" + from __future__ import annotations from influxdb_client_3 import InfluxDBClient3, Point, WritePrecision as _WritePrecision from typing import Any, NamedTuple, Iterable @@ -19,9 +20,13 @@ __all__ = [ WritePrecision = _WritePrecision + class InfluxDBv3Session: """Context manager for InfluxDB v3 session.""" - def __init__(self, host: str, org: str, bucket: str, token: str, verify_ssl: bool = True): + + def __init__( + self, host: str, org: str, bucket: str, token: str, verify_ssl: bool = True + ): """Initialize InfluxDB v3 session. Args: @@ -37,22 +42,29 @@ class InfluxDBv3Session: self._host = host self._token = token self._verify_ssl = verify_ssl - self._client = InfluxDBClient3(self._host, self._org, self._bucket, self._token, verify_ssl=self._verify_ssl) + self._client = InfluxDBClient3( + self._host, + self._org, + self._bucket, + self._token, + verify_ssl=self._verify_ssl, + ) def __enter__(self) -> InfluxDBv3Session: return self def __exit__( - self, - exc_type: type[BaseException] | None, - exc_value: BaseException | None, - exc_traceback: TracebackType | None, - ) -> None: + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + exc_traceback: TracebackType | None, + ) -> None: self._client.close() - - def write(self, bucket: str, - record:str + def write( + self, + bucket: str, + record: str | Iterable[str] | Point | Iterable[Point] @@ -62,7 +74,8 @@ class InfluxDBv3Session: | Iterable[bytes] | NamedTuple | Iterable[NamedTuple], - write_precision: WritePrecision | None = None)->None: + write_precision: WritePrecision | None = None, + ) -> None: """Write records to InfluxDB v3. Args: @@ -76,7 +89,9 @@ class InfluxDBv3Session: self._client.write(record) @classmethod - def from_config_file(cls, path: str | PathLike | None = None)->"InfluxDBv3Session": + def from_config_file( + cls, path: str | PathLike | None = None + ) -> "InfluxDBv3Session": """Create InfluxDBv3Session from configuration file. Args: @@ -94,9 +109,7 @@ class InfluxDBv3Session: InfluxDBv3Session: An instance of InfluxDBv3Session. """ if path is not None: - _config = InfluxDBv3Config( - config_sources=FileSource(path) - ) + _config = InfluxDBv3Config(config_sources=FileSource(path)) else: _config = InfluxDBv3Config( config_sources=FileSource( @@ -108,5 +121,5 @@ class InfluxDBv3Session: org=_config.org, bucket=_config.bucket, token=_config.token.get_secret_value(), - verify_ssl=_config.verify_ssl + verify_ssl=_config.verify_ssl, ) diff --git a/tests/test_create_config.py b/tests/test_create_config.py index c281a71..d05a15d 100644 --- a/tests/test_create_config.py +++ b/tests/test_create_config.py @@ -1,4 +1,3 @@ - import pytest from confz import BaseConfig, FileSource diff --git a/tests/test_influxdbv3_session.py b/tests/test_influxdbv3_session.py index e02cc51..e124c7d 100644 --- a/tests/test_influxdbv3_session.py +++ b/tests/test_influxdbv3_session.py @@ -136,8 +136,6 @@ def test_influxdbv3session_write(influxdb_container): assert result["value"][0].as_py() == 23.5 # type: ignore - - def test_from_config_file_initialization(influxdbv3_config_yaml): """ Test that InfluxDBv3Session.from_config_file initializes correctly from config file.