From 7110d1b42d1001afc77aee35a48a56fac675f397 Mon Sep 17 00:00:00 2001 From: lucas-nelson-uiuc Date: Mon, 30 Dec 2024 15:19:58 -0600 Subject: [PATCH] update source code --- housing_market_context.json | 18 +++++++++ src/tidy_tools/dataframe/context.py | 62 ++++++++++++----------------- src/tidy_tools/dataframe/handler.py | 12 ++++++ src/tidy_tools/model/convert.py | 4 +- 4 files changed, 57 insertions(+), 39 deletions(-) create mode 100644 housing_market_context.json diff --git a/housing_market_context.json b/housing_market_context.json new file mode 100644 index 0000000..e426721 --- /dev/null +++ b/housing_market_context.json @@ -0,0 +1,18 @@ +{ + "name": "Housing Market", + "count": true, + "display": true, + "limit": 10, + "log_handlers": [ + { + "sink": "simple.log", + "level": "INFO", + "format": "{time} | {level} | {message}" + }, + { + "sink": "housing_market.log", + "level": "INFO", + "format": "{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}" + } + ] +} diff --git a/src/tidy_tools/dataframe/context.py b/src/tidy_tools/dataframe/context.py index 275816d..8ec9500 100644 --- a/src/tidy_tools/dataframe/context.py +++ b/src/tidy_tools/dataframe/context.py @@ -1,11 +1,10 @@ import json -from contextlib import contextmanager from pathlib import Path -from typing import ContextManager import attrs from attrs import define from attrs import field +from loguru import logger from tidy_tools.dataframe.handler import TidyLogHandler @@ -85,48 +84,37 @@ def load(cls, context: str | Path | dict) -> "TidyContext": context = json.load(fp) return TidyContext(**context) - def save(self) -> dict: + def save(self, filepath: str | Path | None = None) -> dict | None: """ - Save attributes as serialized JSON object. - - Returns - ------- - dict - Attributes of `TidyContext` instance as dictionary. - """ - return attrs.asdict(self) - - def save_to_file(self, filepath: str | Path) -> None: - """ - Save attributes to `filepath`. + Save attributes as serialized object. Parameters ---------- - filepath : str | Path - File to save attributes. This file can be loaded using the - `TidyContext.load()` method to create copies of the same - instance. + filepath : str | Path | None + Optional path to save context configuration. This file can be + loaded using the `TidyContext.load()` method to + deterministically create copies of the same instance. Returns ------- - None - Stores output to file. + dict | None + If no `filepath` is provided, the attributes of `TidyContext` + instance as dictionary. Else, write configurations to `filepath`. + + Raises + ------ + Exception + If there is an error while writing to `filepath`. """ + context = attrs.asdict(self) + if filepath is None: + return context if not isinstance(filepath, Path): filepath = Path(filepath).resolve() - filepath.write_text(self.save()) - - -@contextmanager -def tidyworkflow(save: str | bool = False, **parameters) -> ContextManager: - context = TidyContext(**parameters) - try: - yield context - finally: - if not save: - pass - if isinstance(save, bool): - return attrs.asdict(context) - if isinstance(save, str): - file = Path(save).resolve() - file.write_text(attrs.asdict(context)) + try: + with open(filepath, "w") as fp: + json.dump(self.save(), fp) + logger.success(f"Context stored at: {filepath}") + except Exception as e: + logger.error(f"Error writing context to: {filepath}") + raise e diff --git a/src/tidy_tools/dataframe/handler.py b/src/tidy_tools/dataframe/handler.py index b45247a..5f5ec08 100644 --- a/src/tidy_tools/dataframe/handler.py +++ b/src/tidy_tools/dataframe/handler.py @@ -2,6 +2,7 @@ from pathlib import Path from typing import TextIO +import attrs from attrs import define from attrs import field from loguru import logger @@ -55,6 +56,17 @@ def pattern(self, pattern: str = LOG_PATTERN) -> str: """ return pattern + def save(self) -> dict: + """ + Return configurations as dict. + + Returns + ------- + dict + Collection of attributes for context. + """ + return attrs.asdict(self) + def summarize(self) -> dict: """ Summarize contents at `source`. diff --git a/src/tidy_tools/model/convert.py b/src/tidy_tools/model/convert.py index 2dad133..2838012 100644 --- a/src/tidy_tools/model/convert.py +++ b/src/tidy_tools/model/convert.py @@ -58,7 +58,7 @@ def convert_field(cls_field: attrs.Attribute, cls_field_exists: bool) -> Column: column = F.to_date(column, format=date_format) else: logger.warning( - f"No `format` provided for {cls_field.name}. Please add `field(..., metadata={{'format': ...}}) and rerun." + f"No `format` provided for {cls_field.name}. Please add `field(..., metadata={{'format': ...}})` and rerun." ) column = column.cast(cls_field_type) case T.TimestampType(): @@ -67,7 +67,7 @@ def convert_field(cls_field: attrs.Attribute, cls_field_exists: bool) -> Column: column = F.to_timestamp(column, format=timestamp_format) else: logger.warning( - f"No `format` provided for {cls_field.name}. Please add `field(..., metadata={{'format': ...}}) and rerun." + f"No `format` provided for {cls_field.name}. Please add `field(..., metadata={{'format': ...}})` and rerun." ) column = column.cast(cls_field_type)