Skip to content

Commit b078454

Browse files
committed
chore: copilot review changes
1 parent 77a6535 commit b078454

10 files changed

Lines changed: 49 additions & 23 deletions

File tree

cosmotech/coal/cosmotech_api/apis/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _download_part(self, dataset_id, dataset_part, destination):
7979
)
8080

8181
@staticmethod
82-
def path_to_parts(_path, part_type) -> list[tuple[str, str, Path, DatasetPartTypeEnum]]:
82+
def path_to_parts(_path, part_type) -> list[tuple[str, Path, DatasetPartTypeEnum]]:
8383
if (_path := Path(_path)).is_dir():
8484
return list((str(_p.relative_to(_path)), _p, part_type) for _p in _path.rglob("*") if _p.is_file())
8585
return list(((_path.name, _path, part_type),))

cosmotech/coal/store/csv.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ def store_csv_file(
1616
table_name: str,
1717
csv_path: pathlib.Path,
1818
replace_existsing_file: bool = False,
19-
store=Store(),
19+
store: Store | None = None,
2020
):
21+
if store is None:
22+
store = Store()
2123
if not csv_path.exists():
2224
raise FileNotFoundError(f"File {csv_path} does not exists")
2325

@@ -32,8 +34,10 @@ def convert_store_table_to_csv(
3234
table_name: str,
3335
csv_path: pathlib.Path,
3436
replace_existsing_file: bool = False,
35-
store=Store(),
37+
store: Store | None = None,
3638
):
39+
if store is None:
40+
store = Store()
3741
if csv_path.name.endswith(".csv") and csv_path.exists() and not replace_existsing_file:
3842
raise FileExistsError(f"File {csv_path} already exists")
3943
if not csv_path.name.endswith(".csv"):

cosmotech/coal/store/native_python.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ def store_pylist(
1414
table_name: str,
1515
data: list[dict],
1616
replace_existsing_file: bool = False,
17-
store=Store(),
17+
store: Store | None = None,
1818
):
19+
if store is None:
20+
store = Store()
1921
data = pa.Table.from_pylist(data)
2022

2123
store.add_table(table_name=table_name, data=data, replace=replace_existsing_file)
2224

2325

24-
def convert_table_as_pylist(table_name: str, store=Store()):
26+
def convert_table_as_pylist(table_name: str, store: Store | None = None):
27+
if store is None:
28+
store = Store()
2529
return store.get_table(table_name).to_pylist()

cosmotech/coal/store/pandas.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ def store_dataframe(
1515
table_name: str,
1616
dataframe: pd.DataFrame,
1717
replace_existsing_file: bool = False,
18-
store=Store(),
18+
store: Store | None = None,
1919
):
20+
if store is None:
21+
store = Store()
2022
data = pyarrow.Table.from_pandas(dataframe)
2123

2224
store.add_table(table_name=table_name, data=data, replace=replace_existsing_file)
2325

2426

25-
def convert_store_table_to_dataframe(table_name: str, store=Store()) -> pd.DataFrame:
27+
def convert_store_table_to_dataframe(table_name: str, store: Store | None = None) -> pd.DataFrame:
28+
if store is None:
29+
store = Store()
2630
return store.get_table(table_name).to_pandas()

cosmotech/coal/store/parquet.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ def store_parquet_file(
1717
table_name: str,
1818
parquet_path: pathlib.Path,
1919
replace_existsing_file: bool = False,
20-
store=Store(),
20+
store: Store | None = None,
2121
):
22+
if store is None:
23+
store = Store()
2224
if not parquet_path.exists():
2325
raise FileNotFoundError(f"File {parquet_path} does not exists")
2426

@@ -33,8 +35,10 @@ def convert_store_table_to_parquet(
3335
table_name: str,
3436
parquet_path: pathlib.Path,
3537
replace_existsing_file: bool = False,
36-
store=Store(),
38+
store: Store | None = None,
3739
):
40+
if store is None:
41+
store = Store()
3842
if parquet_path.name.endswith(".parquet") and parquet_path.exists() and not replace_existsing_file:
3943
raise FileExistsError(f"File {parquet_path} already exists")
4044
if not parquet_path.name.endswith(".parquet"):

cosmotech/coal/store/pyarrow.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ def store_table(
1414
table_name: str,
1515
data: pa.Table,
1616
replace_existsing_file: bool = False,
17-
store=Store(),
17+
store: Store | None = None,
1818
):
19+
if store is None:
20+
store = Store()
1921
store.add_table(table_name=table_name, data=data, replace=replace_existsing_file)
2022

2123

22-
def convert_store_table_to_dataframe(table_name: str, store=Store()) -> pa.Table:
24+
def convert_store_table_to_dataframe(table_name: str, store: Store | None = None) -> pa.Table:
25+
if store is None:
26+
store = Store()
2327
return store.get_table(table_name)

cosmotech/coal/utils/input_collector.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def fetch_parameter(self, param_name: str) -> Path:
2121
def fetch_workspace_file(self, file_name: str) -> Path:
2222
return self.workspace_collector.fetch(file_name)
2323

24-
def fetch(self, name: str) -> Path:
24+
def fetch(self, name: str) -> Path | str:
2525
try:
2626
return self.fetch_parameter(name)
2727
except (KeyError, FileNotFoundError):
@@ -38,8 +38,11 @@ def __init__(self):
3838
self.paths: dict[str, Path] = {}
3939

4040
def collect(self):
41-
for dataset_id in os.listdir(EC.cosmotech.dataset_absolute_path):
42-
for r, d, f in os.walk(Path(EC.cosmotech.dataset_absolute_path) / dataset_id):
41+
base_path = Path(EC.cosmotech.dataset_absolute_path)
42+
for dataset_id in os.listdir(base_path):
43+
if not (base_path / dataset_id).is_dir():
44+
continue
45+
for r, d, f in os.walk(base_path / dataset_id):
4346
for dataset_name in f:
4447
path = Path(r) / dataset_name
4548
self.paths[dataset_name] = path
@@ -89,15 +92,18 @@ def read_parameters_json(self):
8992
self.parameters[parameter["parameterId"]] = parameter["value"]
9093

9194
def collect(self):
92-
for dataset_id in os.listdir(EC.cosmotech.parameters_absolute_path):
93-
for r, d, f in os.walk(Path(EC.cosmotech.parameters_absolute_path) / dataset_id):
95+
base_path = Path(EC.cosmotech.parameters_absolute_path)
96+
for dataset_id in os.listdir(base_path):
97+
if not (base_path / dataset_id).is_dir():
98+
continue
99+
for r, d, f in os.walk(base_path / dataset_id):
94100
for file_name in f:
95101
path = Path(r) / file_name
96102
param_name = path.parent.name
97103
self.paths[param_name] = path
98104
self.paths[path.stem] = path
99105

100-
def fetch_parameter(self, param_name: str) -> Path:
106+
def fetch_parameter(self, param_name: str) -> Path | str:
101107
# lazy collection to avoid unnecessary json loading
102108
if not self.parameters:
103109
self.read_parameters_json()
@@ -111,7 +117,7 @@ def fetch_file_path(self, param_name: str) -> Path:
111117
return self.paths[param_name]
112118
raise FileNotFoundError(f"File for {param_name} not found in {EC.cosmotech.parameters_absolute_path}.")
113119

114-
def fetch(self, param_name: str) -> Path:
120+
def fetch(self, param_name: str) -> Path | str:
115121
try:
116122
return self.fetch_parameter(param_name)
117123
except KeyError:

cosmotech/csm_data/commands/store/load_csv_folder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def load_csv_folder(store_folder, csv_folder):
4141
from cosmotech.coal.utils.logger import LOGGER
4242

4343
_conf = Configuration()
44-
4544
_conf.coal.store = store_folder
4645

46+
store = Store(False, _conf)
4747
for csv_path in pathlib.Path(csv_folder).glob("*.csv"):
4848
LOGGER.info(T("coal.services.azure_storage.found_file").format(file=csv_path.name))
49-
store_csv_file(csv_path.name[:-4], csv_path, store=Store(False, _conf))
49+
store_csv_file(csv_path.name[:-4], csv_path, store=store)

cosmotech/csm_data/commands/store/load_parquet_folder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def load_parquet_folder(store_folder, parquet_folder):
4141
from cosmotech.coal.utils.logger import LOGGER
4242

4343
_conf = Configuration()
44-
4544
_conf.coal.store = store_folder
4645

46+
store = Store(False, _conf)
4747
for parquet_path in pathlib.Path(parquet_folder).glob("*.parquet"):
4848
LOGGER.info(T("coal.services.azure_storage.found_file").format(file=parquet_path.name))
49-
store_parquet_file(parquet_path.name[:-8], parquet_path, store=Store(False, _conf))
49+
store_parquet_file(parquet_path.name[:-8], parquet_path, store=store)

tests/unit/coal/test_utils/test_utils_input_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_fetch_returns_value_if_already_in_parameters(self, tmp_path, mock_ec):
164164
assert result == "value"
165165

166166
def test_fetch_falls_back_to_file_without_lazy_load(self, tmp_path, mock_ec):
167-
"""fetch() does NOT trigger read_parameters_json — falls straight to fetch_file_path."""
167+
"""fetch() delegates to fetch_parameter(), which loads parameters.json FIRST and returns the JSON value."""
168168
param_dir = tmp_path / "myparam"
169169
param_dir.mkdir()
170170
f = param_dir / "data.csv"

0 commit comments

Comments
 (0)