Skip to content

Commit b614dec

Browse files
authored
Merge pull request #415 from Duncan-Hunter/main
db optional dependency
2 parents 0a35cf2 + 1fabe2b commit b614dec

File tree

14 files changed

+1852
-1434
lines changed

14 files changed

+1852
-1434
lines changed

.github/workflows/dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535

3636
- name: Install dependencies
3737
run: |
38-
python -m pip install --upgrade pip
38+
python -m pip install -e '.[db]' --upgrade pip
3939
pip install poetry tox tox-gh-actions
4040
4141
- name: test with tox

.github/workflows/preview.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ on:
1111
# Allows you to run this workflow manually from the Actions tab
1212
workflow_dispatch:
1313

14+
permissions:
15+
contents: write
16+
id-token: write
17+
1418
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
1519
jobs:
1620
publish_dev_build:
@@ -50,9 +54,9 @@ jobs:
5054
poetry build
5155
5256
- name: publish to Test PyPI
53-
uses: pypa/gh-action-pypi-publish@master
57+
uses: pypa/gh-action-pypi-publish@release/v1
5458
with:
5559
user: __token__
5660
password: ${{ secrets.TEST_PYPI_API_TOKEN}}
5761
repository_url: https://test.pypi.org/legacy/
58-
skip_existing: true
62+
skip-existing: true

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ENV POETRY_INSTALLER_MAX_WORKERS=10
5555

5656
# Install project in editable mode and with development dependencies
5757
WORKDIR $APP_PATH
58-
RUN poetry install
58+
RUN poetry install --all-extras
5959

6060
ENTRYPOINT ["poetry", "run"]
6161
CMD ["nml"]

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ docker -v /local/config/dir/:/config/ run nannyml/nannyml nml run
149149
python -m pip install git+https://github.com/NannyML/nannyml
150150
```
151151

152+
#### Extras
153+
154+
If you're using database connections to read model inputs/outputs or you're exporting monitoring results to a database,
155+
you'll need to include the optional `db` dependency. For example using `pip`:
156+
157+
```bash
158+
pip install nannyml[db]
159+
```
160+
161+
or using `poetry`
162+
163+
```bash
164+
poetry install nannyml --all-extras
165+
```
166+
152167
### Quick Start
153168

154169
_The following snippet is based on our [latest release](https://github.com/NannyML/nannyml/releases/latest)_.

docs/installing_nannyml.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ or
3232

3333
Any issues with installation? `Let us know`_ so we can help you.
3434

35+
Extras
36+
------
37+
38+
If you're using database connections to read model inputs/outputs or you're exporting monitoring results to a database,
39+
you'll need to include the optional `db` dependency. For example using `pip`:
40+
41+
.. code-block:: bash
42+
43+
$ pip install nannyml[db]
44+
45+
or using `poetry`
46+
47+
.. code-block:: bash
48+
49+
$ poetry install nannyml --all-extras
3550
3651
.. _`Let us know`: https://github.com/NannyML/nannyml/issues
3752
.. _`LightGBM`: https://github.com/microsoft/LightGBM

docs/tutorials/working_with_results.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ results to disk using a :class:`~nannyml.io.raw_files_writer.RawFilesWriter`, se
202202
:class:`~nannyml.io.db.database_writer.DatabaseWriter`. This example will show how to use the
203203
:class:`~nannyml.io.db.database_writer.DatabaseWriter`.
204204

205+
In order to get the dependencies required for database access, please ensure you've installed the
206+
optional `db` dependency. Check the :ref:`installation instructions<installing_nannyml>` for more information.
207+
205208
We construct the :class:`~nannyml.io.db.database_writer.DatabaseWriter` by providing a database connection string.
206209
Upon calling the :meth:`~nannyml.io.base.Writer.write` method, all results will be written into
207210
the database, in this case, an `SQLite` database.

nannyml/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131
# Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer.
3232
# 'X.Y.dev0' is the canonical version of 'X.Y.dev'
3333
#
34-
__version__ = '0.11.0'
34+
__version__ = '0.12.0'
3535

3636
import logging
3737

3838
from dotenv import load_dotenv
39+
from importlib import import_module
40+
3941

4042
from .calibration import Calibrator, IsotonicCalibrator, needs_calibration
4143
from .chunk import Chunk, Chunker, CountBasedChunker, DefaultChunker, PeriodBasedChunker, SizeBasedChunker
42-
from .data_quality import MissingValuesCalculator, UnseenValuesCalculator, NumericalRangeCalculator
44+
from .data_quality import MissingValuesCalculator, NumericalRangeCalculator, UnseenValuesCalculator
4345
from .datasets import (
4446
load_modified_california_housing_dataset,
4547
load_synthetic_binary_classification_dataset,
@@ -59,7 +61,7 @@
5961
UnivariateDriftCalculator,
6062
)
6163
from .exceptions import ChunkerException, InvalidArgumentsException, MissingMetadataException
62-
from .io import DatabaseWriter, PickleFileWriter, RawFilesWriter
64+
from .io import PickleFileWriter, RawFilesWriter
6365
from .performance_calculation import PerformanceCalculator
6466
from .performance_estimation import CBPE, DLE
6567
from .stats import (
@@ -71,6 +73,20 @@
7173
)
7274
from .usage_logging import UsageEvent, disable_usage_logging, enable_usage_logging, log_usage
7375

76+
77+
_optional_dependencies = {
78+
'DatabaseWriter': '.io.db',
79+
}
80+
81+
82+
def __getattr__(name: str):
83+
optional_module_path = _optional_dependencies.get(name)
84+
if optional_module_path is not None:
85+
module = import_module(optional_module_path, package=__name__)
86+
return getattr(module, name)
87+
raise AttributeError(f"module {__name__} has no attribute {name}")
88+
89+
7490
try:
7591
import nannyml_premium
7692

nannyml/io/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@
1212
1313
The `store` module implements an object cache, meant to cache fitted calculators in between runs.
1414
"""
15+
from importlib import import_module
1516

1617
from .base import Reader, Writer, WriterFactory
17-
from .db import DatabaseWriter
1818
from .file_reader import FileReader
1919
from .file_writer import FileWriter
2020
from .pickle_file_writer import PickleFileWriter
2121
from .raw_files_writer import RawFilesWriter
2222
from .store import FilesystemStore, JoblibPickleSerializer, Serializer, Store
2323

24+
25+
_optional_dependencies = {
26+
'DatabaseWriter': '.db'
27+
}
28+
29+
30+
def __getattr__(name):
31+
optional_module_path = _optional_dependencies.get(name)
32+
if optional_module_path is not None:
33+
module = import_module(optional_module_path, package=__name__)
34+
return getattr(module, name)
35+
raise AttributeError(f"module {__name__} has no attribute {name}")
36+
37+
2438
DEFAULT_WRITER = RawFilesWriter(path='out')

nannyml/io/db/database_writer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from typing import Any, Dict, Optional
22

3-
from sqlmodel import Session, SQLModel, create_engine, select
3+
try:
4+
from sqlmodel import Session, SQLModel, create_engine, select
5+
except ImportError:
6+
raise ImportError(
7+
"`sqlmodel` module is not available. Please install the `nannyml[db]` extra to use this functionality."
8+
)
49

510
from nannyml._typing import Result
611
from nannyml.exceptions import WriterException

nannyml/io/db/entities.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
"""
1010

1111
import sys
12-
1312
from datetime import datetime
1413
from typing import List, Optional
1514

1615
from pydantic import ConfigDict
17-
from sqlmodel import Field, Relationship, SQLModel
16+
17+
try:
18+
from sqlmodel import Field, Relationship, SQLModel
19+
except ImportError:
20+
raise ImportError(
21+
"`sqlmodel` module is not available. Please install the `nannyml[db]` extra to use this functionality."
22+
)
1823

1924

2025
class Model(SQLModel, table=True): # type: ignore[call-arg]

0 commit comments

Comments
 (0)