Skip to content

Commit

Permalink
Updated all the builders to use a dataclass rather than a tuple:
Browse files Browse the repository at this point in the history
likely to be necessary for passing around coordinates as well as
data variables as we begin to make coordinates indexable - I think
we'll begin to get confused about what belongs where.
  • Loading branch information
charles-turner-1 committed Sep 24, 2024
1 parent f36050f commit ca1743e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 202 deletions.
24 changes: 12 additions & 12 deletions src/access_nri_intake/catalog/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CatalogManager:
Add/update intake sources in an intake-dataframe-catalog like the ACCESS-NRI catalog
"""

def __init__(self, path : str):
def __init__(self, path: str):
"""
Initialise a CatalogManager instance to add/update intake sources in a
intake-dataframe-catalog like the ACCESS-NRI catalog
Expand Down Expand Up @@ -58,14 +58,14 @@ def __init__(self, path : str):

def build_esm(
self,
name : str,
description : str,
name: str,
description: str,
builder,
path : list[str] | str,
path: list[str] | str,
translator=DefaultTranslator,
metadata : dict | None = None,
directory : str | None = None,
overwrite : bool =False,
metadata: dict | None = None,
directory: str | None = None,
overwrite: bool = False,
**kwargs,
):
"""
Expand Down Expand Up @@ -124,12 +124,12 @@ def build_esm(

def load(
self,
name : str,
description : str,
path : str,
driver : str ="esm_datastore",
name: str,
description: str,
path: str,
driver: str = "esm_datastore",
translator=DefaultTranslator,
metadata : dict | None =None,
metadata: dict | None = None,
**kwargs,
):
"""
Expand Down
25 changes: 12 additions & 13 deletions src/access_nri_intake/catalog/translators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
"""

from __future__ import annotations

from functools import partial
from typing import Callable, TYPE_CHECKING
from intake import DataSource
from typing import Callable

import pandas as pd
import tlz
from intake import DataSource

from . import COLUMNS_WITH_ITERABLES

if TYPE_CHECKING:
from intake import DataSource

class TranslatorError(Exception):
"Generic Exception for the Translator classes"
Expand All @@ -30,7 +29,7 @@ class DefaultTranslator:
of metadata for use in an intake-dataframe-catalog.
"""

def __init__(self, source : DataSource, columns : list[str]):
def __init__(self, source: DataSource, columns: list[str]):
"""
Initialise a DefaultTranslator. This Translator works as follows:
Expand All @@ -50,7 +49,7 @@ def __init__(self, source : DataSource, columns : list[str]):

self.source = source
self.columns = columns
self._dispatch : dict[str, Callable[[],pd.Series]] = {
self._dispatch: dict[str, Callable[[], pd.Series]] = {
column: partial(self._default_translator, column=column)
for column in columns
}
Expand Down Expand Up @@ -101,7 +100,7 @@ def _default_translator(self, column: str) -> pd.Series:

return pd.Series([val] * len_df)

def translate(self, groupby : list[str] | None = None) -> pd.DataFrame:
def translate(self, groupby: list[str] | None = None) -> pd.DataFrame:
"""
Return the translated :py:class:`~pandas.DataFrame` of metadata and merge into set of
set of rows with unique values of the columns specified.
Expand Down Expand Up @@ -154,7 +153,7 @@ class Cmip6Translator(DefaultTranslator):
CMIP6 Translator for translating metadata from the NCI CMIP6 intake datastores.
"""

def __init__(self, source : DataSource, columns :list[str]):
def __init__(self, source: DataSource, columns: list[str]):
"""
Initialise a Cmip6Translator
Expand Down Expand Up @@ -202,7 +201,7 @@ class Cmip5Translator(DefaultTranslator):
CMIP5 Translator for translating metadata from the NCI CMIP5 intake datastores.
"""

def __init__(self, source : DataSource, columns : list[str]):
def __init__(self, source: DataSource, columns: list[str]):
"""
Initialise a Cmip5Translator
Expand Down Expand Up @@ -250,7 +249,7 @@ class EraiTranslator(DefaultTranslator):
ERAI Translator for translating metadata from the NCI ERA-Interim intake datastore.
"""

def __init__(self, source : DataSource, columns : list[str]):
def __init__(self, source: DataSource, columns: list[str]):
"""
Initialise a EraiTranslator
Expand All @@ -272,7 +271,7 @@ def _variable_translator(self):
return _to_tuple(self.source.df["variable"])


def _cmip_frequency_translator(series : pd.Series) -> pd.Series:
def _cmip_frequency_translator(series: pd.Series) -> pd.Series:
"""
Return frequency from CMIP frequency metadata
"""
Expand Down Expand Up @@ -305,7 +304,7 @@ def _cmip_realm_translator(series) -> pd.Series:
realms per cmip asset
"""

def _translate(string : str) -> tuple[str, ...]:
def _translate(string: str) -> tuple[str, ...]:
translations = {
"na": "none",
"landonly": "land",
Expand All @@ -324,7 +323,7 @@ def _translate(string : str) -> tuple[str, ...]:
return series.apply(lambda string: _translate(string))


def _to_tuple(series : pd.Series) -> pd.Series:
def _to_tuple(series: pd.Series) -> pd.Series:
"""
Make each entry in the provided series a tuple
Expand Down
Loading

0 comments on commit ca1743e

Please sign in to comment.