Skip to content

replace constant dicts with namedtuples #257

Closed

Description

@smoors

this will be a life changing improvement that must get top priority.

example:

from typing import NamedTuple

class DeviceTypes(NamedTuple):
    CPU: str = 'cpu'
    GPU: str = 'gpu'

DEVICE_TYPES = DeviceTypes()

print(DEVICE_TYPES.GPU)  # 'gpu'

advantages:

  • really constant, you cannot change the value directly
  • you only have to import DEVICE_TYPES, not CPU or GPU
  • strings 'CPU' and 'GPU' don't have to be defined anymore
  • thus, you cannot mistakenly use the 'CPU' or 'GPU' strings anymore
  • easier syntax DEVICE_TYPES.CPU instead of DEVICE_TYPES[CPU]: saves a full character!

for SCALES this is probably not a good idea, as we really want to use the strings themselves, e.g. on the cmd line.

Activity

  1. smoors commented on Mar 14, 2025

    @smoors
    CollaboratorAuthor

    thinking more about the SCALES, maybe it is still useful to have constants for the strings, and it doesn't prevent you from still using the strings directly if you want?

    EDIT: scratch that, Python doesn't allow variables that start with a number, like 1_NODE

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions