From ab1c98f58b4d0c32928963274efebcf20f247f8c Mon Sep 17 00:00:00 2001 From: Samuel Jackson <samueljackson@outlook.com> Date: Wed, 12 Apr 2017 10:18:44 +0100 Subject: [PATCH] Refs #19349 Expose methods on CrystalStructure --- .../geometry/src/Exports/CrystalStructure.cpp | 54 ++++++++++++++++++- .../mantid/geometry/CrystalStructureTest.py | 21 ++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/Framework/PythonInterface/mantid/geometry/src/Exports/CrystalStructure.cpp b/Framework/PythonInterface/mantid/geometry/src/Exports/CrystalStructure.cpp index 58cb8d7e661..0231845401b 100644 --- a/Framework/PythonInterface/mantid/geometry/src/Exports/CrystalStructure.cpp +++ b/Framework/PythonInterface/mantid/geometry/src/Exports/CrystalStructure.cpp @@ -1,5 +1,6 @@ #include "MantidGeometry/Crystal/CrystalStructure.h" #include "MantidGeometry/Crystal/IsotropicAtomBraggScatterer.h" +#include <boost/algorithm/string/join.hpp> #include <boost/python/class.hpp> #include <boost/python/make_constructor.hpp> #include <boost/python/register_ptr_to_python.hpp> @@ -13,7 +14,7 @@ SpaceGroup_sptr getSpaceGroup(CrystalStructure &self) { return boost::const_pointer_cast<SpaceGroup>(self.spaceGroup()); } -std::vector<std::string> getScatterers(CrystalStructure &self) { +std::vector<std::string> getScatterers(const CrystalStructure &self) { CompositeBraggScatterer_sptr scatterers = self.getScatterers(); std::vector<std::string> scattererStrings; @@ -26,6 +27,53 @@ std::vector<std::string> getScatterers(CrystalStructure &self) { return scattererStrings; } + +std::string __str__implementation(const CrystalStructure& self) { + std::stringstream ss; + ss << "Crystal structure with:\n"; + ss << "Unit cell:"; + + const auto cell = self.cell(); + ss << " a = " << cell.a(); + ss << " b = " << cell.b(); + ss << " c = " << cell.c(); + + ss << " alpha = " << cell.alpha(); + ss << " beta = " << cell.beta(); + ss << " gamma = " << cell.gamma(); + + ss << "\n"; + + ss << "Centering: " << self.centering()->getName() << "\n"; + ss << "Space Group: " << self.spaceGroup()->hmSymbol() << "\n"; + ss << "Scatters: " << boost::algorithm::join(getScatterers(self), ", "); + + return ss.str(); +} + +std::string __repr__implementation(const CrystalStructure& self) { + std::stringstream ss; + ss << "CrystalStructure(\""; + + const auto cell = self.cell(); + + ss << cell.a() << " "; + ss << cell.b() << " "; + ss << cell.c() << " "; + + ss << cell.alpha() << " "; + ss << cell.beta() << " "; + ss << cell.gamma(); + ss << "\", "; + + ss << "\"" << self.spaceGroup()->hmSymbol() << "\", "; + ss << "\"" << boost::algorithm::join(getScatterers(self), "; ") << "\""; + + ss << ")"; + + return ss.str(); +} + } void export_CrystalStructure() { @@ -34,5 +82,7 @@ void export_CrystalStructure() { (arg("unitCell"), arg("spaceGroup"), arg("scatterers")))) .def("getUnitCell", &CrystalStructure::cell, arg("self")) .def("getSpaceGroup", &getSpaceGroup, arg("self")) - .def("getScatterers", &getScatterers, arg("self")); + .def("getScatterers", &getScatterers, arg("self")) + .def("__str__", &__str__implementation) + .def("__repr__", &__repr__implementation); } diff --git a/Framework/PythonInterface/test/python/mantid/geometry/CrystalStructureTest.py b/Framework/PythonInterface/test/python/mantid/geometry/CrystalStructureTest.py index b756e2961aa..c52f6da116a 100644 --- a/Framework/PythonInterface/test/python/mantid/geometry/CrystalStructureTest.py +++ b/Framework/PythonInterface/test/python/mantid/geometry/CrystalStructureTest.py @@ -56,6 +56,27 @@ class CrystalStructureTest(unittest.TestCase): self.assertEqual(';'.join(scatterers), initialString) + def test_to_string(self): + initialString = "Al 1/3 0.454 1/12 1 0.01;Si 0.1 0.2 0.3 0.99 0.1" + structure = CrystalStructure("5.43 5.42 5.41", "F d -3 m", initialString) + + expected_str = "Crystal structure with:\nUnit cell: a = 5.43 b = 5.42 "\ + "c = 5.41 alpha = 90 beta = 90 gamma = 90\n"\ + "Centering: All-face centred\nSpace Group: F d -3 m\n"\ + "Scatters: Al 1/3 0.454 1/12 1 0.01, "\ + "Si 0.1 0.2 0.3 0.99 0.1" + + expected_repr = "CrystalStructure(\"5.43 5.42 5.41 90 90 90\", "\ + "\"F d -3 m\", \"Al 1/3 0.454 1/12 1 0.01; "\ + "Si 0.1 0.2 0.3 0.99 0.1\")" + + self.assertEqual(expected_str, str(structure)) + self.assertEqual(expected_repr, structure.__repr__()) + + newStructure = eval(structure.__repr__()) + self.assertEqual(structure.getUnitCell().a(), newStructure.getUnitCell().a()) + + if __name__ == '__main__': unittest.main() -- GitLab