# slurm_script_generator.slurm_script

*module*

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1-L1)

## slurm_script_generator.slurm_script.nodes

*attribute* · *module attribute*

```python
nodes = pragmas.Nodes(1)
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1189-L1189)

## slurm_script_generator.slurm_script.pragma

*attribute* · *module attribute*

```python
pragma = pragmas.Account('max')
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1188-L1188)

## slurm_script_generator.slurm_script.script

*attribute* · *module attribute*

```python
script = SlurmScript(account='max', nodes=1, modules=['gcc/12', 'openmpi/4.1'], custom_commands=['source ~/virtual_envs/env_slurm/bin/activate', 'mpirun -n 4 ./bin > run.out'])
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1190-L1198)

## slurm_script_generator.slurm_script.script2

*attribute* · *module attribute*

```python
script2 = SlurmScript.from_dict(slurm_dict)
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1203-L1203)

## slurm_script_generator.slurm_script.slurm_dict

*attribute* · *module attribute*

```python
slurm_dict = script.to_dict()
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1200-L1200)

## slurm_script_generator.slurm_script.SlurmScript

*class*

```python
class SlurmScript
```

Class representing a Slurm batch script with pragmas, modules, and custom commands.

Parameters
----------
account : str, optional
    The account to charge for the job.
array : str, optional
    The job array specification.
begin : str, optional
    The time to begin the job.
bell : str, optional
    Ring terminal bell when job is allocated.
burst_buffer : str, optional
    Burst buffer specifications.
bb_file : str, optional
    Burst buffer specification file.
cpus_per_task : int, optional
    Number of CPUs required per task.
comment : str, optional
    Arbitrary comment for the job.
container : str, optional
    Path to OCI container bundle.
container_id : str, optional
    OCI container ID.
cpu_freq : str, optional
    Requested CPU frequency and governor.
delay_boot : str, optional
    Delay boot for desired node features.
dependency : str, optional
    Job dependency specification.
deadline : str, optional
    Remove job if no ending possible before deadline.
chdir : str, optional
    Change working directory for the job.
get_user_env : str, optional
    Used by Moab for environment setup.
gres : str, optional
    Required generic resources.
gres_flags : str, optional
    Flags related to GRES management.
hold : str, optional
    Submit job in held state.
immediate : str, optional
    Exit if resources not available within seconds.
job_name : str, optional
    Name of the job.
no_kill : str, optional
    Do not kill job on node failure.
kill_command : str, optional
    Signal to send terminating job.
licenses : str, optional
    Required licenses, comma separated.
clusters : str, optional
    Comma separated list of clusters.
distribution : str, optional
    Distribution method for processes.
mail_type : str, optional
    Notify on state change.
mail_user : str, optional
    Email for job state changes.
mcs_label : str, optional
    MCS label if mcs plugin is used.
ntasks : str, optional
    Number of processors required.
nice : str, optional
    Decrease scheduling priority by value.
nodes : int, optional
    Number of nodes to allocate.
ntasks_per_node : int, optional
    Number of tasks to invoke on each node.
oom_kill_step : str, optional
    Set OOMKillStep behaviour.
overcommit : str, optional
    Overcommit resources.
power : str, optional
    Power management options.
priority : str, optional
    Set job priority.
profile : str, optional
    Enable acct_gather_profile for detailed data.
partition : str, optional
    Partition requested.
qos : str, optional
    Quality of service.
quiet : str, optional
    Suppress informational messages.
reboot : str, optional
    Reboot compute nodes before starting job.
oversubscribe : str, optional
    Oversubscribe resources with other jobs.
signal : str, optional
    Send signal when time limit within seconds.
spread_job : str, optional
    Spread job across as many nodes as possible.
error : str, optional
    Redirect stderr to file.
output : str, optional
    Redirect stdout to file.
switches : str, optional
    Optimum switches and max wait time.
core_spec : str, optional
    Count of reserved cores.
thread_spec : str, optional
    Count of reserved threads.
time : str, optional
    Time limit for the job.
time_min : str, optional
    Minimum time limit.
tres_bind : str, optional
    Task to TRES binding options.
tres_per_task : str, optional
    TRES required per task.
use_min_nodes : str, optional
    Prefer smaller node count.
wckey : str, optional
    Wckey to run job under.
cluster_constraint : str, optional
    List of cluster constraints.
contiguous : str, optional
    Demand contiguous range of nodes.
constraint : str, optional
    List of constraints.
nodefile : str, optional
    Request specific list of hosts from file.
mem : str, optional
    Minimum real memory required.
mincpus : str, optional
    Minimum logical processors per node.
reservation : str, optional
    Allocate resources from named reservation.
tmp : str, optional
    Minimum temporary disk required.
nodelist : str, optional
    Request specific list of hosts.
exclude : str, optional
    Exclude specific list of hosts.
exclusive_user : str, optional
    Allocate nodes in exclusive mode.
exclusive_mcs : str, optional
    Exclusive mode when mcs plugin enabled.
mem_per_cpu : str, optional
    Real memory per allocated CPU.
resv_ports : str, optional
    Reserve communication ports.
sockets_per_node : int, optional
    Number of sockets per node to allocate.
cores_per_socket : int, optional
    Number of cores per socket to allocate.
threads_per_core : int, optional
    Number of threads per core to allocate.
extra_node_info : str, optional
    Combine sockets, cores, threads.
ntasks_per_core : int, optional
    Number of tasks per core.
ntasks_per_socket : int, optional
    Number of tasks per socket.
hint : str, optional
    Application binding hints.
mem_bind : str, optional
    Bind memory to locality domains.
cpus_per_gpu : int, optional
    Number of CPUs required per allocated GPU.
gpus : str, optional
    Count of GPUs required.
gpu_bind : str, optional
    Task to GPU binding options.
gpu_freq : str, optional
    Frequency and voltage of GPUs.
gpus_per_node : str, optional
    GPUs per allocated node.
gpus_per_socket : str, optional
    GPUs per allocated socket.
gpus_per_task : str, optional
    GPUs per spawned task.
mem_per_gpu : str, optional
    Real memory per allocated GPU.
disable_output_job_summary : str, optional
    Disable job summary in output file.
nvmps : str, optional
    Launch NVIDIA MPS for job.
pragmas : List[Pragma], optional
    List of pragmas to add to the script.
modules : List[str], optional
    List of modules to load in the script.
custom_command : str, optional
    Custom command to run in the script.
custom_commands : list, optional
    List of custom commands to run in the script.
inlined_script : str, optional
    Inline script to include in the batch script.
inlined_scripts : list, optional
    List of inline scripts to include in the batch script.
line_length : int, optional
    Line length for formatting output.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L18-L1178)

### slurm_script_generator.slurm_script.SlurmScript.custom_commands

*property*

```python
custom_commands: List[str]
```

Get the list of custom commands to run in the script.

Parameters
----------

Returns
-------
List[str]
    List of custom command strings.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1124-L1136)

### slurm_script_generator.slurm_script.SlurmScript.line_length

*property*

```python
line_length: int
```

Get the maximum line length for the script, used for formatting the output.
This value is used to determine how many characters fit on a line
when generating the script string, and is also used for formatting
the header and section separators.

Parameters
----------

Returns
-------
int
    The line length value.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1073-L1088)

### slurm_script_generator.slurm_script.SlurmScript.modules

*property*

```python
modules: List[str]
```

Get the list of modules to load in the script.

Parameters
----------

Returns
-------
List[str]
    List of module names.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1109-L1121)

### slurm_script_generator.slurm_script.SlurmScript.pragmas

*property*

```python
pragmas: List[Pragma]
```

Get the list of Pragma objects in the script.

Parameters
----------

Returns
-------
List[Pragma]
    List of all Pragma instances.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1091-L1106)

### slurm_script_generator.slurm_script.SlurmScript.add_custom_command

*method*

```python
def add_custom_command(command: str) -> None
```

Add a single custom command to the script.

Parameters
----------
command : str
    The custom command to add.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L451-L467)

### slurm_script_generator.slurm_script.SlurmScript.add_custom_commands

*method*

```python
def add_custom_commands(commands: List[str] | None) -> None
```

Add multiple custom commands to the script.

Parameters
----------
commands : list of str, optional
    List of custom commands to add.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L469-L485)

### slurm_script_generator.slurm_script.SlurmScript.add_inlined_script

*method*

```python
def add_inlined_script(path: str) -> None
```

Add lines from an inlined script file to the custom commands.

Parameters
----------
path : str
    Path to the script file to inline.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L526-L547)

### slurm_script_generator.slurm_script.SlurmScript.add_inlined_scripts

*method*

```python
def add_inlined_scripts(paths: List[str] | None) -> None
```

Add lines from multiple inlined script files to the custom commands.

Parameters
----------
paths : list of str, optional
    List of script file paths to inline.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L549-L565)

### slurm_script_generator.slurm_script.SlurmScript.add_module

*method*

```python
def add_module(module: str) -> None
```

Add a single module to the script.

Parameters
----------
module : str
    The module to add.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L488-L504)

### slurm_script_generator.slurm_script.SlurmScript.add_modules

*method*

```python
def add_modules(modules: List[str] | None) -> None
```

Add multiple modules to the script.

Parameters
----------
modules : list of str, optional
    List of modules to add.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L506-L523)

### slurm_script_generator.slurm_script.SlurmScript.add_param

*method*

```python
def add_param(key: str, value: Any) -> None
```

Add a non-pragma parameter to the script.

Parameters
----------
key : str
    The parameter key.
value : Any
    The parameter value.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L608-L639)

### slurm_script_generator.slurm_script.SlurmScript.add_pragma

*method*

```python
def add_pragma(pragma: Pragma) -> None
```

Add a Pragma object to the script, replacing any existing pragma with the same destination.

Parameters
----------
pragma : Pragma
    The Pragma object to add.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L568-L587)

### slurm_script_generator.slurm_script.SlurmScript.add_pragmas

*method*

```python
def add_pragmas(pragmas: List[Pragma] | None) -> None
```

Add multiple Pragma objects to the script.

Parameters
----------
pragmas : list of Pragma, optional
    List of Pragma objects to add.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L589-L606)

### slurm_script_generator.slurm_script.SlurmScript.check

*method*

```python
def check() -> None
```

Raise ``ValueError`` if :meth:`validate` finds any problems.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L758-L765)

### slurm_script_generator.slurm_script.SlurmScript.from_dict

*method* · *staticmethod*

```python
def from_dict(data: dict[str, Any]) -> SlurmScript
```

Create a SlurmScript instance from a dictionary.

Parameters
----------
data : dict[str, Any]
    Dictionary containing the SlurmScript data.

Returns
-------
SlurmScript
    The constructed SlurmScript object.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L894-L921)

### slurm_script_generator.slurm_script.SlurmScript.from_json

*method* · *staticmethod*

```python
def from_json(path: str) -> SlurmScript
```

Load a SlurmScript instance from a JSON file.

Parameters
----------
path : str
    Path to the JSON file to load.

Returns
-------
SlurmScript
    The constructed SlurmScript object.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1037-L1054)

### slurm_script_generator.slurm_script.SlurmScript.from_script

*method* · *staticmethod*

```python
def from_script(script: str, verbose: bool = False) -> SlurmScript
```

Parse a SLURM script string and create a SlurmScript instance.

Parameters
----------
script : str
    SLURM script content.
verbose: bool :
     (Default value = False)

Returns
-------
SlurmScript
    The constructed SlurmScript object.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L944-L1019)

### slurm_script_generator.slurm_script.SlurmScript.generate_script

*method*

```python
def generate_script(line_length: int = 54, include_header: bool = False) -> str
```

Parameters
----------
line_length :
    int:  (Default value = 54)
include_header :
    bool:  (Default value = False)

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L641-L721)

### slurm_script_generator.slurm_script.SlurmScript.read_script

*method* · *staticmethod*

```python
def read_script(path: str, verbose: bool = False) -> SlurmScript
```

Read a SLURM script from a file and parse it into a SlurmScript instance.

Parameters
----------
path : str
    Path to the script file.
verbose : bool
    Whether to enable verbose output. (Default value = False)

Returns
-------
SlurmScript
    The parsed SlurmScript object.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L923-L942)

### slurm_script_generator.slurm_script.SlurmScript.save

*method*

```python
def save(path: str | Path, include_header: bool = True, verbose: bool = False) -> None
```

Save the generated SLURM script to a file.

Parameters
----------
path : str
    Path to save the script file.
include_header : bool
    Whether to include the script header.
verbose : bool
    Whether to enable verbose output. (Default value = False)

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L785-L811)

### slurm_script_generator.slurm_script.SlurmScript.submit_job

*method*

```python
def submit_job(path: str, verbose: bool = False, tail: bool = False) -> int
```

Submit the SLURM script as a job using sbatch.

Parameters
----------
path : str
    Path to the script file to submit.
verbose : bool, optional
    Print submission details.
tail : bool, optional
    Follow the file configured by ``--output`` until the submitted job
    leaves the queue. The output filename may contain Slurm's ``%j``
    (job ID) and ``%x`` (job name) substitutions. Defaults to False.

Returns
-------

Raises
------
ValueError
    If :meth:`validate` finds a combination of pragmas sbatch will
    reject (e.g. both ``--mem`` and ``--mem-per-cpu`` set).
RuntimeError
    If sbatch fails to submit the job.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L813-L852)

### slurm_script_generator.slurm_script.SlurmScript.to_dict

*method*

```python
def to_dict() -> dict[str, Any]
```

Convert the SlurmScript instance to a dictionary representation.

Parameters
----------

Returns
-------
dict[str, Any]
    Dictionary with keys 'pragmas', 'modules', and 'custom_commands'.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L767-L783)

### slurm_script_generator.slurm_script.SlurmScript.to_json

*method*

```python
def to_json(path: str) -> None
```

Save the SlurmScript instance as a JSON file.

Parameters
----------
path : str
    Path to save the JSON file.

Returns
-------

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1021-L1035)

### slurm_script_generator.slurm_script.SlurmScript.to_string

*method*

```python
def to_string(include_header: bool = True) -> str
```

Generate the SLURM script as a string.

Parameters
----------
include_header : bool
    Whether to include the header in the generated script. (Default value = True)

Returns
-------
str
    The generated script string.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L1056-L1070)

### slurm_script_generator.slurm_script.SlurmScript.validate

*method*

```python
def validate() -> List[str]
```

Check for combinations of pragmas that ``sbatch`` will reject.

These are mistakes that only surface once a job is actually
submitted (or silently misbehave), so it is worth catching them in
Python first. This is not an exhaustive validator of sbatch's rules,
just the handful of cross-pragma mistakes that are easy to make.

Returns
-------
List[str]
    Human-readable problem descriptions. Empty if nothing was found.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/slurm_script.py#L723-L756)
