From 78c6be94a9ec79cdd3a8a40a70fe33bd7796156e Mon Sep 17 00:00:00 2001 From: Samuel Jackson <samueljackson@outlook.com> Date: Thu, 13 Apr 2017 07:40:31 +0100 Subject: [PATCH] Refs #19349 Expose methods on PointGroup --- .../inc/MantidGeometry/Crystal/PointGroup.h | 2 ++ Framework/Geometry/src/Crystal/PointGroup.cpp | 12 ++++++++++++ .../mantid/geometry/src/Exports/PointGroup.cpp | 15 ++++++++++++++- .../test/python/mantid/geometry/PointGroupTest.py | 15 +++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h b/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h index 9c65a1294ef..8a962e176d9 100644 --- a/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h +++ b/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h @@ -96,6 +96,8 @@ MANTID_GEOMETRY_DLL PointGroup::LatticeSystem getLatticeSystemFromString(const std::string &latticeSystem); +MANTID_GEOMETRY_DLL std::ostream &operator<<(std::ostream& stream, const PointGroup& self); + /// This is necessary to make the map work with older compilers. Can be removed /// when GCC 4.4 is not used anymore. struct MANTID_GEOMETRY_DLL CrystalSystemComparator { diff --git a/Framework/Geometry/src/Crystal/PointGroup.cpp b/Framework/Geometry/src/Crystal/PointGroup.cpp index b5b0bdab0d7..12d118eaceb 100644 --- a/Framework/Geometry/src/Crystal/PointGroup.cpp +++ b/Framework/Geometry/src/Crystal/PointGroup.cpp @@ -320,5 +320,17 @@ operator()(const PointGroup::CrystalSystem &lhs, return static_cast<int>(lhs) < static_cast<int>(rhs); } +/// Returns a streamed representation of the PointGroup object +std::ostream &operator<<(std::ostream& stream, const PointGroup& self) { + stream << "Point group with\n" + << "Lattice system: " + << getLatticeSystemAsString(self.latticeSystem()) << "\n" + << "Crystal system: " + << getCrystalSystemAsString(self.crystalSystem()) << "\n" + << "Symbol: " + << self.getSymbol(); + return stream; +} + } // namespace Mantid } // namespace Geometry diff --git a/Framework/PythonInterface/mantid/geometry/src/Exports/PointGroup.cpp b/Framework/PythonInterface/mantid/geometry/src/Exports/PointGroup.cpp index 0fa7083a7da..b7a242dd8f6 100644 --- a/Framework/PythonInterface/mantid/geometry/src/Exports/PointGroup.cpp +++ b/Framework/PythonInterface/mantid/geometry/src/Exports/PointGroup.cpp @@ -4,6 +4,8 @@ #include "MantidPythonInterface/kernel/Converters/PyObjectToV3D.h" #include <boost/python/class.hpp> +#include <boost/python/self.hpp> +#include <boost/python/operators.hpp> #include <boost/python/enum.hpp> #include <boost/python/scope.hpp> #include <boost/python/list.hpp> @@ -40,6 +42,15 @@ boost::python::list getEquivalents(PointGroup &self, const object &hkl) { Mantid::Kernel::V3D getReflectionFamily(PointGroup &self, const object &hkl) { return self.getReflectionFamily(Converters::PyObjectToV3D(hkl)()); } + +std::string __repr__implementation(const PointGroup &self) { + std::stringstream ss; + ss << "PointGroupFactory.createPointGroup(\""; + ss << self.getSymbol(); + ss << "\")"; + return ss.str(); +} + } void export_PointGroup() { @@ -79,5 +90,7 @@ void export_PointGroup() { "HKL.") .def("getReflectionFamily", &getReflectionFamily, (arg("self"), arg("hkl")), - "Returns the same HKL for all symmetry equivalents."); + "Returns the same HKL for all symmetry equivalents.") + .def(str(self)) + .def("__repr__", &__repr__implementation); } diff --git a/Framework/PythonInterface/test/python/mantid/geometry/PointGroupTest.py b/Framework/PythonInterface/test/python/mantid/geometry/PointGroupTest.py index 673fa2da4f9..a95f1b70ebb 100644 --- a/Framework/PythonInterface/test/python/mantid/geometry/PointGroupTest.py +++ b/Framework/PythonInterface/test/python/mantid/geometry/PointGroupTest.py @@ -53,6 +53,21 @@ class PointGroupTest(unittest.TestCase): self.assertEquals(pg_rhombohedral.getLatticeSystem(), PointGroup.LatticeSystem.Rhombohedral) self.assertEquals(pg_hexagonal.getLatticeSystem(), PointGroup.LatticeSystem.Hexagonal) + def test_to_string(self): + pg = PointGroupFactory.createPointGroup("m-3m") + + expected_str = "Point group with\nLattice system: Cubic"\ + "\nCrystal system: Cubic\nSymbol: m-3m" + + expected_repr = "PointGroupFactory.createPointGroup(\"m-3m\")" + + self.assertEqual(expected_str, str(pg)) + self.assertEqual(expected_repr, pg.__repr__()) + + newPg = eval(pg.__repr__()) + self.assertEqual(pg.getHMSymbol(), newPg.getHMSymbol()) + + if __name__ == '__main__': unittest.main() -- GitLab