Python Hostlist

2.3.0 · active · verified Fri Apr 17

python-hostlist is a Python module designed for parsing and expanding hostlist strings (e.g., 'host[01-03],host[05,07]'). It can expand such strings into a list of individual hostnames and also compress a list of hostnames back into a compact hostlist string. The library is actively maintained, with releases typically driven by bug fixes or minor feature additions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic expansion and compression of hostlist strings, and checking for hostlist validity.

import hostlist

# Expand a hostlist string
expanded_hosts = hostlist.expand_hostlist('host[01-03,05],server[10-12]')
print(f"Expanded: {expanded_hosts}")

# Compress a list of hostnames
hostnames = ['node01', 'node02', 'node03', 'node05', 'node07']
compressed_string = hostlist.compress_hostlist(hostnames)
print(f"Compressed: {compressed_string}")

# Check if a string is a valid hostlist
is_valid = hostlist.is_hostlist('test[1-5]')
print(f"Is 'test[1-5]' a valid hostlist? {is_valid}")
is_invalid = hostlist.is_hostlist('test[1-a]')
print(f"Is 'test[1-a]' a valid hostlist? {is_invalid}")

view raw JSON →