NCAS-CMS / NCAS-CMS/cf-python

Refactor semantic constants to enumerations

Open
#942 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

code tidy
Dominant language
Python
Stars
150
Forks
23
Avg merge
1d 11h
Merged PRs (30d)
2

Description

In code review at https://github.com/NCAS-CMS/cf-python/pull/910#discussion_r2975606868 I noted that we could replace a tuple defining valid indexing schemes with an equivalent enumeration:

https://github.com/NCAS-CMS/cf-python/blob/02e348c5c84784e6a1613c3722a3c2ed1a0fd25d/cf/functions.py#L3501

We agreed we should at some point survey the codebase systematically for further appropriate places to use enumerations to e.g. make comparisons against well-defined sets of values such as controlled vocabs more robust, clear and type safe.

We already have Enums applied well for a few cases:

https://github.com/NCAS-CMS/cf-python/blob/8c760f82ed119cdda0ad041bc9deec49f3bc15d4/cf/constants.py#L522-L542

From a quick look, some further good candidates across the codebase for using these are:

  1. Also in the constants module as above: the CF cell methods set, and perhaps the keys from the cr_coordinates dict and the formula coordinate types (atmosphere_sigma_coordinate, ocean_s_coordinate, ocean_sigma_z_coordinate, etc.) both of which appear across multiple dictionaries there (which could in turn be converted to data classes, also an improvement);
  2. The valid regridding methods, i.e. keys to:
    https://github.com/NCAS-CMS/cf-python/blob/02e348c5c84784e6a1613c3722a3c2ed1a0fd25d/cf/regrid/regrid.py#L20-L29
  3. (Upstream in cfdm*) the valid backends for reading and writing: https://github.com/NCAS-CMS/cfdm/blob/e6ebddb4a0f533f079f7f1ea0da2efdf6595c928/cfdm/read_write/netcdf/netcdfread.py#L1334-L1339 (for netCDF only) and https://github.com/NCAS-CMS/cfdm/blob/e6ebddb4a0f533f079f7f1ea0da2efdf6595c928/cfdm/read_write/netcdf/netcdfwrite.py#L5768 respectively.

* we should consider for all Enums defined whether it is best to define and import them in from cfdm, or have them in cf-python only.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the tuple in cf/functions.py, the existing Enum definitions in cf/constants.py, and the valid regridding methods in cf/regrid/regrid.py. Survey the listed constants and repeated controlled-value sets, then determine a focused scope and whether definitions belong in cf-python or cfdm. Done means the agreed semantic constants use appropriate enumerations without breaking existing comparisons or interfaces.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.