Add getitem and eq to slurm objects

This commit is contained in:
Benoît Seignovert 2024-02-19 17:53:35 +01:00
parent e8ae32cc86
commit 02166be4d2
Signed by: Benoît Seignovert
GPG key ID: F5D8895227D18A0B
2 changed files with 31 additions and 20 deletions

View file

@ -40,6 +40,9 @@ class SlurmGpu:
def __str__(self): def __str__(self):
return self.name return self.name
def __eq__(self, other):
return str(self) == str(other)
@dataclass @dataclass
class SlurmNode: class SlurmNode:
@ -65,6 +68,9 @@ class SlurmNode:
def __str__(self): def __str__(self):
return self.hostname return self.hostname
def __eq__(self, other):
return str(self) == str(other)
@dataclass @dataclass
class SlurmPartition: class SlurmPartition:
@ -76,14 +82,17 @@ class SlurmPartition:
def __str__(self): def __str__(self):
return self.name return self.name
def __eq__(self, other):
return str(self) == str(other)
def __iter__(self): def __iter__(self):
return iter(self.nodes) return iter(self.nodes)
def __len__(self): def __len__(self):
return len(self.nodes) return len(self.nodes)
def __eq__(self, other): def __getitem__(self, other):
return str(self) == str(other) return self.nodes[self.nodes.index(other)]
@property @property
def gpus(self) -> str: def gpus(self) -> str:
@ -111,14 +120,17 @@ class SlurmCluster:
def __str__(self): def __str__(self):
return self.name return self.name
def __eq__(self, other):
return str(self) == str(other)
def __iter__(self): def __iter__(self):
return iter(self.partitions) return iter(self.partitions)
def __len__(self): def __len__(self):
return len(self.partitions) return len(self.partitions)
def __eq__(self, other): def __getitem__(self, other):
return str(self) == str(other) return self.partitions[self.partitions.index(other)]
def sinfo_run(username: str = None) -> str: def sinfo_run(username: str = None) -> str:

View file

@ -38,8 +38,7 @@ def test_slurm_dataclasses():
gpu = SlurmGpu('fOo', '1') gpu = SlurmGpu('fOo', '1')
assert gpu # __bool__ assert gpu # __bool__
assert str(gpu) == 'Foo' # = name assert gpu == str(gpu) == gpu.name == 'Foo'
assert gpu.name == 'Foo'
assert gpu.nb == 1 assert gpu.nb == 1
assert isinstance(gpu.name, str) assert isinstance(gpu.name, str)
@ -49,17 +48,15 @@ def test_slurm_dataclasses():
gpu = SlurmGpu() gpu = SlurmGpu()
assert not gpu # __bool__ assert not gpu # __bool__
assert str(gpu) == 'None' # = name assert str(gpu) == gpu.name == 'None'
assert gpu.name == 'None'
assert gpu.nb == 0 assert gpu.nb == 0
# Node # Node
node = SlurmNode(*'nautilus standard cnode001 completing 0/96/0/96 384000 (null)'.split()) node = SlurmNode(*'nautilus standard cnode001 completing 0/96/0/96 384000 (null)'.split())
assert str(node) == 'cnode001' # hostname
assert node.cluster == 'nautilus' assert node.cluster == 'nautilus'
assert node.partition == 'standard' assert node.partition == 'standard'
assert node.hostname == 'cnode001' assert node == str(node) == node.hostname == 'cnode001'
assert node.state == 'completing' assert node.state == 'completing'
assert node.cpu.allocated == 0 assert node.cpu.allocated == 0
assert node.cpu.idle == 96 assert node.cpu.idle == 96
@ -70,14 +67,15 @@ def test_slurm_dataclasses():
# Partition # Partition
partition = SlurmPartition('standard', [node]) partition = SlurmPartition('standard', [node])
assert str(partition) == 'standard' # = name assert partition == str(partition) == partition.name == 'standard'
assert partition.name == 'standard'
assert partition == 'standard' # __eq__ on name
assert len(partition) == 1 assert len(partition) == 1
for _node in partition: for _node in partition:
assert str(_node) == 'cnode001' assert _node == 'cnode001'
_node = partition['cnode001'] # __getitem__
assert isinstance(_node, SlurmNode)
assert _node == 'cnode001'
assert partition.gpus == 'None' assert partition.gpus == 'None'
assert partition.max_idle_cpu == 96 assert partition.max_idle_cpu == 96
@ -86,14 +84,15 @@ def test_slurm_dataclasses():
# Cluster # Cluster
cluster = SlurmCluster('nautilus', [partition]) cluster = SlurmCluster('nautilus', [partition])
assert str(cluster) == 'nautilus' # = name assert cluster == str(cluster) == cluster.name == 'nautilus'
assert cluster.name == 'nautilus'
assert cluster == 'nautilus' # __eq__ on name
assert len(cluster) == 1 assert len(cluster) == 1
for _partition in cluster: for _partition in cluster:
assert str(_partition) == 'standard' assert _partition == 'standard'
_partition = cluster['standard'] # __getitem__
assert isinstance(_partition, SlurmPartition)
assert _partition == 'standard'
def test_slurm_sinfo_run(monkeypatch): def test_slurm_sinfo_run(monkeypatch):