charmed-hpc / slurmutils

Utilities and APIs for interfacing with the Slurm workload manager ⚙️🔌
GNU Lesser General Public License v3.0
1 stars 3 forks source link

[Enhancement]: Support mapping `"yes"`/`"no"` to Python boolean values #22

Open NucciTheBoss opened 1 day ago

NucciTheBoss commented 1 day ago

Slurm configuration format uses "yes"/"no" to represent boolean values, similar to how older versions of YAML did. However, working with the 'Slurmy' boolean values in Python is not straight forward as they are treated as strings when parsed in from Slurm configuration files. To evaluate whether that values are boolean, you must use string operations rather than boolean operations.

if config.constrain_ram_space == "yes":
    print("no more ram for you 0_0")

This isn't ideal as this isn't a True/False evaluation. config.constrain_ram_space can be set to anything here, not just the opposite of "yes" which is "no":

config.constrain_ram_space = "well maybe"
if config.constrain_ram_space == "yes":
    print("no more ram for you 0_0")

cgroupconfig.dump(config, "/etc/slurm/cgroup.conf")  #=> Sadness

The editor won't catch this which will eventually result in borking Slurm.

We should support mapping these values to Python booleans. It should be too difficult as all you need to do is just add a callback in the callback module that just converts the Slurm boolens back and forth:

def to_bool(v: str) -> bool:
    return v.lower() == "yes"

def from_bool(v: bool) -> str:
    return "yes" if v else "no"