Skip to content

Commit cbadf3e

Browse files
hakonanespavoljuhas
authored andcommitted
Add a space group string representation, tested
Signed-off-by: Håkon Wiik Ånes <[email protected]>
1 parent 839020a commit cbadf3e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/diffpy/structure/spacegroupmod.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,19 @@ def __init__(self,
262262
self.pdb_name = pdb_name
263263
self.symop_list = symop_list
264264

265+
def __repr__(self):
266+
"""Return a string representation of the space group."""
267+
return (
268+
"SpaceGroup #%i (%s, %s). Symmetry matrices: %i, "
269+
"point sym. matr.: %i"
270+
) % (
271+
self.number,
272+
self.short_name,
273+
self.crystal_system[0] + self.crystal_system[1:].lower(),
274+
self.num_sym_equiv,
275+
self.num_primitive_sym_equiv,
276+
)
277+
265278

266279
def iter_symops(self):
267280
"""Iterate over all symmetry operations in the space group.

src/diffpy/structure/tests/testspacegroups.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ def test__hashSymOpList(self):
8686
self.assertEqual(len(SpaceGroupList), len(hset))
8787
return
8888

89+
def test_spacegroup_representation(self):
90+
"""Verify SpaceGroup.__repr__()."""
91+
numbers = [1, 3, 16, 75, 143, 168, 229]
92+
short_names = ["P1", "P2", "P222", "P4", "P3", "P6", "Im-3m"]
93+
systems = [
94+
"Triclinic",
95+
"Monoclinic",
96+
"Orthorhombic",
97+
"Tetragonal",
98+
"Trigonal",
99+
"Hexagonal",
100+
"Cubic",
101+
]
102+
n_symmetry_matrices = [1, 2, 4, 4, 3, 6, 96]
103+
n_point_symmetry_matrices = [1, 2, 4, 4, 3, 6, 48]
104+
sg_dict = dict(zip(
105+
numbers,
106+
zip(short_names, systems, n_symmetry_matrices, n_point_symmetry_matrices)
107+
))
108+
for key, value in sg_dict.items():
109+
sg = GetSpaceGroup(key)
110+
expected_str = (
111+
"SpaceGroup #%i (%s, %s). Symmetry matrices: %i, "
112+
"point sym. matr.: %i"
113+
) % ((key,) + value)
114+
115+
self.assertEqual(sg.__repr__(), expected_str)
116+
117+
89118
# End of class TestRoutines
90119

91120
# ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)