Skip to content

Commit 0fd08cd

Browse files
committed
Cleanup
1 parent e27668a commit 0fd08cd

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

‎cf_xarray/accessor.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ def _get_bounds(obj: DataArray | Dataset, key: Hashable) -> list[Hashable]:
497497

498498

499499
@functools.lru_cache(maxsize=256)
500-
def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hashable]]:
500+
def _parse_grid_mapping_attribute(
501+
grid_mapping_attr: str,
502+
) -> Mapping[str, list[Hashable]]:
501503
"""
502504
Parse a grid_mapping attribute that may contain multiple grid mappings.
503505
@@ -509,19 +511,20 @@ def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hash
509511
- Multiple: "spatial_ref: crs_4326: latitude longitude crs_27700: x27700 y27700"
510512
-> {"spatial_ref": [], "crs_4326": ["latitude", "longitude"], "crs_27700": ["x27700", "y27700"]}
511513
512-
Returns a dictionary mapping grid mapping variable names to their associated coordinate variables.
514+
Returns a read-only mapping from grid mapping variable name to its associated
515+
coordinate variables. The result is memoized, so callers must not mutate it.
513516
"""
514517
# Check if there are colons indicating multiple mappings
515518
if ":" not in grid_mapping_attr:
516-
return {grid_mapping_attr.strip(): []}
519+
return Frozen({grid_mapping_attr.strip(): []})
517520

518521
# Use regex to parse the format
519522
# First, find all grid mapping variables (words before colons)
520523
grid_pattern = r"(?:^|\s)([a-zA-Z_][a-zA-Z0-9_]*)(?=\s*:)"
521524
grid_mappings = re.findall(grid_pattern, grid_mapping_attr)
522525

523526
if not grid_mappings:
524-
return {grid_mapping_attr.strip(): []}
527+
return Frozen({grid_mapping_attr.strip(): []})
525528

526529
result: dict[str, list[Hashable]] = {}
527530

@@ -556,7 +559,7 @@ def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hash
556559
def _create_grid_mapping(
557560
var_name: str,
558561
ds: Dataset,
559-
grid_mapping_dict: dict[str, list[Hashable]],
562+
grid_mapping_dict: Mapping[str, list[Hashable]],
560563
) -> GridMapping:
561564
"""
562565
Create a GridMapping dataclass instance from a grid mapping variable.

‎cf_xarray/utils.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ def parse_cell_methods_attr(attr: str) -> Mapping[str, str]:
7171
"""
7272
Parse cell_methods attributes (format is 'measure: name').
7373
74-
The result is memoized per attribute string and returned as a read-only
75-
``Frozen`` mapping. ``cell_measures`` strings are typically shared across
76-
many variables in a dataset (e.g., every CMIP variable carries the same
77-
``area: areacella volume: ...``), so parsing once and reusing avoids
78-
repeated string splitting in the hot path of ``_get_measure``.
79-
8074
Parameters
8175
----------
8276
attr : str

0 commit comments

Comments
 (0)