Skip to content

Add constants/enums for field values #172

Description

@Mr0grog

We have a (semi) standard set of values that are available for each of:

  • Race/Ethnicity
  • Gender
  • Comorbidities/Underlying Conditions
  • Transmission Categories

(See the data model docs for more on this. Note that age does not have standardized values like this because the categories are so different across counties that we can’t meaningfully bucket them.)

Rather than having strings that could contain typos for these all over the codebase, we should really have a set of constants or (better yet) enums for them, e.g:

# This part should be in a centralized module somewhere.
from enum import Enum
class RaceEthnicity(Enum):
    african_american = "African_Amer"
    asian = "Asian"
    latinx = "Latinx_or_Hispanic"
    native_american = "Native_Amer"
    pacific_islander = "Pacific_Islander"
    white = "White"
    multiple = "Multiple_Race"
    other = "Other"
    unknown = "Unknown"

# Elsewhere in some scraper code...
# This example from Marin (https://github.com/sfbrigade/data-covid19-sfbayarea/blob/5fb99a51fa89ddc3ffacf65c3ae1c1fda19e75e9/covid19_sfbayarea/data/marin.py#L190-L206)

# Map the county's naming scheme to ours:
mapping = {
    'american indian/alaska native': RaceEthnicity.native_american,
    'asian': RaceEthnicity.asian,
    'black/african american': RaceEthnicity.african_american,
    'hispanic/latinx': RaceEthnicity.latinx,
    'multiracial': RaceEthnicity.multiple,
    'native hawaiian/pacific islander': RaceEthnicity.pacific_islander,
    'unknown': RaceEthnicity.unknown,
    'other': RaceEthnicity.other,
    'white': RaceEthnicity.white,
}
data = get_demographic_totals(api, 'Race')
return {mapping[row['grouping'].lower()]: int(row['cumulative'])
        for row in data}

This way, mypy can check that we haven’t made any typos and our values are consistent across the codebase.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions