From d787e943df481b871e098ee8da569a3700c40a13 Mon Sep 17 00:00:00 2001 From: "Josef M. Gallmetzer" Date: Mon, 5 Aug 2024 21:13:40 +0200 Subject: [PATCH] Refactor: Add __len__ method to AtomicSystem class --- PQAnalysis/atomic_system/atomic_system.py | 11 +++++++++++ tests/atomicSystem/test_atomic_system.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/PQAnalysis/atomic_system/atomic_system.py b/PQAnalysis/atomic_system/atomic_system.py index cca73fa..7729a4e 100644 --- a/PQAnalysis/atomic_system/atomic_system.py +++ b/PQAnalysis/atomic_system/atomic_system.py @@ -680,6 +680,17 @@ def __getitem__( topology=self.topology[keys] ) + def __len__(self) -> int: + """ + Returns the number of atoms in the AtomicSystem. + + Returns + ------- + int + The number of atoms in the AtomicSystem. + """ + return self.n_atoms + def __str__(self) -> str: """ Returns the string representation of the AtomicSystem. diff --git a/tests/atomicSystem/test_atomic_system.py b/tests/atomicSystem/test_atomic_system.py index 9bcc15d..4e73580 100644 --- a/tests/atomicSystem/test_atomic_system.py +++ b/tests/atomicSystem/test_atomic_system.py @@ -356,6 +356,20 @@ def test_n_atoms(self, caplog): system ) + def test__len__(self): + system = AtomicSystem() + assert len(system) == 0 + + system = AtomicSystem(pos=np.array([[0, 0, 0], [1, 1, 1]])) + assert len(system) == 2 + + system = AtomicSystem( + pos=np.array([[0, 0, 0], [1, 1, 1]]), + atoms=[Atom('C'), Atom('H')], + cell=Cell(0.75, 0.75, 0.75) + ) + assert len(system) == 2 + def test__str__(self): """ Test the __str__ method of the AtomicSystem class.