Skip to content

Commit

Permalink
Export the UnitFactory to the new Python API. Refs #5624
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jul 17, 2012
1 parent d9eb9e1 commit cce51cb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ set ( TEST_PY_FILES
test/python/QuatTest.py
test/python/V3DTest.py
test/python/UnitCellTest.py
test/python/UnitFactoryTest.py
test/python/WorkspaceFactoryTest.py
test/python/WorkspaceTest.py
test/python/WorkspaceGroupTest.py
test/python/WorkspacePropertiesTest.py
test/python/WorkspaceValidatorsTest.py

)

set ( PYTEST_HELPERS test/python/testhelpers.py )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set ( EXPORT_FILES
src/Exports/MandatoryValidator.cpp
src/Exports/CompositeValidator.cpp
src/Exports/LogFilter.cpp
src/Exports/UnitFactory.cpp
)

set ( SRC_FILES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
Defines a set of aliases for the kernel module to make
accessing certain objects easier.
"""
from _kernel import ConfigServiceImpl, Logger
from _kernel import ConfigServiceImpl, Logger, UnitFactoryImpl

###############################################################################
# Singletons
# Singletons - Make them just look like static classes
###############################################################################
ConfigService = ConfigServiceImpl.Instance()
config = ConfigService

UnitFactory = UnitFactoryImpl.Instance()

###############################################################################
# Set up a general Python logger. Others can be created as they are required
# if a user wishes to be more specific
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "MantidKernel/UnitFactory.h"

#include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h"

#include <boost/python/class.hpp>
#include <boost/python/return_value_policy.hpp>

using Mantid::Kernel::UnitFactory;
using Mantid::Kernel::UnitFactoryImpl;
namespace Policies = Mantid::PythonInterface::Policies;
namespace Converters = Mantid::PythonInterface::Converters;
using namespace boost::python;

void export_UnitFactory()
{
class_<UnitFactoryImpl, boost::noncopyable>("UnitFactoryImpl", no_init)
.def("getKeys", &UnitFactoryImpl::getKeys, return_value_policy<Policies::VectorToNumpy>(),
"Returns a list of units available from the factory")

.def("Instance", &UnitFactory::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the UnitFactory singleton")
.staticmethod("Instance")
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
from mantid import UnitFactory, UnitFactoryImpl

class UnitFactoryTest(unittest.TestCase):

def test_alias_is_of_type_UnitFactoryImpl(self):
self.assertTrue(isinstance(UnitFactory, UnitFactoryImpl))

def test_keys_returns_a_non_empty_python_list_of_unit_keys(self):
known_units = UnitFactory.getKeys()

self.assertEquals(type(known_units), list)
# Check length is at least the known core units
# but allow for others to be added
core_units = ['Empty', 'Label', 'TOF', 'Wavelength','Energy',
'Energy_inWavenumber', 'dSpacing', 'MomentumTransfer',
'QSquared', 'DeltaE', 'DeltaE_inWavenumber', 'Momentum']
self.assertTrue(len(core_units) <= len(known_units))

for unit in core_units:
self.assertTrue(unit in known_units, "%s unit not found in UnitFactory keys" % unit)

if __name__ == '__main__':
unittest.main()

0 comments on commit cce51cb

Please sign in to comment.