Skip to content

Commit

Permalink
update source code
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-nelson-uiuc committed Dec 30, 2024
1 parent 6184016 commit 7110d1b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
18 changes: 18 additions & 0 deletions housing_market_context.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
62 changes: 25 additions & 37 deletions src/tidy_tools/dataframe/context.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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(<filepath>)` 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(<filepath>)` 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
12 changes: 12 additions & 0 deletions src/tidy_tools/dataframe/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions src/tidy_tools/model/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)

Expand Down

0 comments on commit 7110d1b

Please sign in to comment.