Skip to content

Commit 4151e06

Browse files
Merge pull request #99 from CDCgov/rr-95-use-logging
Rr 95 use logging
2 parents 532ed21 + 44f6348 commit 4151e06

4 files changed

Lines changed: 31 additions & 16 deletions

File tree

cfa/dataops/catalog.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""building a validated datasource namespace"""
22

33
import json
4+
import logging
45
import os
56
import pkgutil
67
from collections.abc import Sequence
@@ -42,6 +43,10 @@
4243
_config = ConfigParser()
4344
_config.read(os.path.join(_here, "config.ini"))
4445

46+
logger = logging.getLogger(__name__)
47+
if not logger.handlers:
48+
logger.addHandler(logging.NullHandler())
49+
4550

4651
def get_all_catalogs() -> list:
4752
"""Get a list of all available dataops catalogs.
@@ -57,8 +62,10 @@ def get_all_catalogs() -> list:
5762
for module_finder, modname, ispkg in pkgutil.iter_modules(catalog_pkg.__path__):
5863
if ispkg:
5964
catalogs.append((catalog_nspace, modname, module_finder.path))
60-
except ModuleNotFoundError:
61-
print(f"No catalogs exist in namespace {catalog_nspace}")
65+
except ModuleNotFoundError as e:
66+
if e.name != catalog_nspace:
67+
raise
68+
logger.warning("No catalogs exist in namespace %s", catalog_nspace)
6269

6370
return catalogs
6471

@@ -340,6 +347,7 @@ def _get_version_blobs(
340347
raise ValueError(
341348
f"Version {version} not found in available versions: {available_versions}"
342349
)
350+
logger.info(f"Using version: {version}")
343351
if print_version:
344352
print(f"Using version: {version}")
345353
if isinstance(version, list):
@@ -424,6 +432,7 @@ def get_dataframe(
424432
output: Literal["pandas", "pd"] = "pandas",
425433
version_spec: str | None = None,
426434
selection: Literal["newest", "oldest"] = "newest",
435+
print_version: bool = False,
427436
) -> pd.DataFrame: ...
428437

429438
@overload
@@ -432,6 +441,7 @@ def get_dataframe(
432441
output: Literal["polars", "pl"],
433442
version_spec: str | None = None,
434443
selection: Literal["newest", "oldest"] = "newest",
444+
print_version: bool = False,
435445
) -> pl.DataFrame: ...
436446

437447
@overload
@@ -440,13 +450,15 @@ def get_dataframe(
440450
output: Literal["pl_lazy", "lazy"],
441451
version_spec: str | None = None,
442452
selection: Literal["newest", "oldest"] = "newest",
453+
print_version: bool = False,
443454
) -> pl.LazyFrame: ...
444455

445456
def get_dataframe(
446457
self,
447458
output: Literal["pandas", "pd", "polars", "pl", "pl_lazy", "lazy"] = "pandas",
448459
version_spec: str | None = None,
449460
selection: Literal["newest", "oldest"] = "newest",
461+
print_version: bool = False,
450462
) -> pd.DataFrame | pl.DataFrame | pl.LazyFrame:
451463
"""Get the data as a pandas or polars dataframe
452464
@@ -456,6 +468,7 @@ def get_dataframe(
456468
version_spec (str, optional): the version of the data to get.
457469
Defaults to "latest".
458470
selection (Literal["newest", "oldest", "all"], optional): whether to get the newest, oldest, or all matching versions. Defaults to "newest".
471+
print_version (bool, optional): whether to log the resolved version information. Defaults to False.
459472
460473
Raises:
461474
ValueError: if output is not one of
@@ -472,7 +485,7 @@ def get_dataframe(
472485
)
473486
# Fetch version blobs once and validate before deriving file extension.
474487
version_blobs = self._get_version_blobs(
475-
version_spec=version_spec, selection=selection
488+
version_spec=version_spec, selection=selection, print_version=print_version
476489
)
477490
if not version_blobs:
478491
raise ValueError(
@@ -627,7 +640,7 @@ def save_dataframe(
627640
)
628641
if file_format in ["json", "jsonl"] and path_after_prefix.endswith(".json"):
629642
path_after_prefix = path_after_prefix[:-5] + ".jsonl"
630-
print("Changing file extension to .jsonl for line-delimited JSON.")
643+
logger.info("Changing file extension to .jsonl for line-delimited JSON.")
631644
if isinstance(df, pd.DataFrame):
632645
if file_format == "parquet":
633646
pq_bytes = df.to_parquet(index=False, compression="snappy")

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Calendar Versioning](https://calver.org/).
77
The versioning pattern is `YYYY.MM.DD.micro(a/b/{none if release})
88

99
---
10+
## [2026.07.20.0]
11+
12+
- change most print statements to logging
13+
1014
## [2026.06.30.0]
1115

1216
- changed SimpleNamespace to CatalogNamespace for more helpful type hints

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "cfa.dataops"
3-
version = "2026.06.30.0"
3+
version = "2026.07.20.0"
44
description = "Data cataloging, ETL, modeling, verification, and validation for CFA"
55
authors = [
66
{ name = "Phil Rogers", email = "ap66@cdc.gov" },

tests/test_blob_endpoint_save_methods.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,23 @@ def test_save_pandas_dataframe_json(self, mocker, blob_endpoint, sample_pandas_d
128128
assert isinstance(call_args[1]["file_buffer"], bytes)
129129

130130
def test_save_pandas_dataframe_json_auto_extension_fix(
131-
self, mocker, blob_endpoint, sample_pandas_df, capsys
131+
self, mocker, blob_endpoint, sample_pandas_df, caplog
132132
):
133133
"""Test that .json extension is automatically changed to .jsonl"""
134134
mock_write = mocker.patch.object(blob_endpoint, "write_blob")
135135

136-
blob_endpoint.save_dataframe(
137-
df=sample_pandas_df,
138-
path_after_prefix="data/output.json",
139-
file_format="json",
140-
auto_version=False,
141-
)
136+
with caplog.at_level("INFO"):
137+
blob_endpoint.save_dataframe(
138+
df=sample_pandas_df,
139+
path_after_prefix="data/output.json",
140+
file_format="json",
141+
auto_version=False,
142+
)
142143

143144
mock_write.assert_called_once()
144145
call_args = mock_write.call_args
145146
assert call_args[1]["path_after_prefix"] == "data/output.jsonl"
146-
147-
# Check that warning message was printed
148-
captured = capsys.readouterr()
149-
assert "Changing file extension to .jsonl" in captured.out
147+
assert "Changing file extension to .jsonl" in caplog.text
150148

151149
def test_save_polars_dataframe_parquet(
152150
self, mocker, blob_endpoint, sample_polars_df

0 commit comments

Comments
 (0)