beam-tracing / Scotty

Beam tracing code for diagnostics
https://scotty.readthedocs.io/en/latest/
GNU General Public License v3.0
7 stars 4 forks source link

Implement input sanitation in beam_me_up.py #58

Closed valerian-chen closed 2 years ago

valerian-chen commented 2 years ago

Check that mode_flag is either 1 or -1 and nothing else

ZedThree commented 2 years ago

Two solutions for you:

1) the simple version:

    if mode_flag not in [-1, 1]:
        raise ValueError(
            f"Bad value for `mode_flag`! Expected either 1 or -1, got '{mode_flag}'"
        )

2) the fancy version:

from enum import IntEnum

class Mode(IntEnum):
  O = 1
  X = -1

mode_flag = Mode(mode_flag)

The second one will error if mode_flag is not a member of Mode, and allows users to specify the name of the mode directly: mode_flag = Mode.O, or with either exactly 1 or -1.