easy_slurm.format#

For formatting job names.

easy_slurm.format.format_with_config(template: str, config: dict[str, Any], silent: bool = False, _now: Optional[datetime.datetime] = None) str[source]#

Formats template using given config.

The template syntax is very similar to Python string templates. One useful addition is that nested config keys can be accessed via “namespace” syntax. For instance,

"{nested.dict.key}"             ==> config["nested"]["dict"]["key"]
"{hp.batch_size:06}"            ==> "000032"

Additionally, some built-in keys have special formatting syntax. If these keys are present in config, they will be ignored. For instance,

"{date:%Y-%m-%d}"               ==> "2020-01-01"
"{date:%Y-%m-%d %H:%M:%S.%3f}"  ==> "2020-01-01 00:00:03.141"

See the examples below.

Parameters
  • template – String to format.

  • config – Key-value data to replace "{key:format_spec}" with.

  • silent – Silently pass-through "{key:format_spec}" if key not in config.

Returns

Formatted string.

Examples:

>>> from datetime import datetime
>>> date_string = "2020-01-01 00:00:03.141592"
>>> now = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S.%f")
>>> config = {"hp": {"batch_size": 32, "lr": 1e-2}}
>>> fmt = "{date:%Y-%m-%d}_bs={hp.batch_size:04},lr={hp.lr:.1e}"
>>> format_with_config(fmt, config, _now=now)
'2020-01-01_bs=0032,lr=1.0e-02'
>>> fmt = "{date:%Y-%m-%d_%H-%M-%S_%3f}_bs={hp.batch_size}"
>>> format_with_config(fmt, config, _now=now)
'2020-01-01_00-00-03_141_bs=32'