Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
mantid
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mantidproject
mantid
Commits
135e3480
Commit
135e3480
authored
8 years ago
by
David Fairbrother
Browse files
Options
Downloads
Patches
Plain Diff
Re #19156 Added case sensitivity option to dict helper
parent
e9086c12
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
scripts/Diffraction/isis_powder/routines/common.py
+20
-4
20 additions, 4 deletions
scripts/Diffraction/isis_powder/routines/common.py
scripts/test/ISISPowderCommonTest.py
+37
-2
37 additions, 2 deletions
scripts/test/ISISPowderCommonTest.py
with
57 additions
and
6 deletions
scripts/Diffraction/isis_powder/routines/common.py
+
20
−
4
View file @
135e3480
from
__future__
import
(
absolute_import
,
division
,
print_function
)
from
__future__
import
(
absolute_import
,
division
,
print_function
)
from
six
import
iterkeys
import
mantid.kernel
as
kernel
import
mantid.kernel
as
kernel
import
mantid.simpleapi
as
mantid
import
mantid.simpleapi
as
mantid
...
@@ -11,7 +12,7 @@ def cal_map_dictionary_key_helper(dictionary, key, append_to_error_message=None)
...
@@ -11,7 +12,7 @@ def cal_map_dictionary_key_helper(dictionary, key, append_to_error_message=None)
message stating the following key could not be found in the calibration mapping file. As
message stating the following key could not be found in the calibration mapping file. As
several instruments will use this message it makes sense to localise it to common. If a
several instruments will use this message it makes sense to localise it to common. If a
message is passed in append_to_error_message it will append that to the end of the generic
message is passed in append_to_error_message it will append that to the end of the generic
error message in its own line when an exception is raised.
error message in its own line when an exception is raised.
This lookup is case insensitive.
:param dictionary: The dictionary to search in for the key
:param dictionary: The dictionary to search in for the key
:param key: The key to search for
:param key: The key to search for
:param append_to_error_message: (Optional) The message to append to the end of the error message
:param append_to_error_message: (Optional) The message to append to the end of the error message
...
@@ -19,7 +20,9 @@ def cal_map_dictionary_key_helper(dictionary, key, append_to_error_message=None)
...
@@ -19,7 +20,9 @@ def cal_map_dictionary_key_helper(dictionary, key, append_to_error_message=None)
"""
"""
err_message
=
"
The field
'"
+
str
(
key
)
+
"'
is required within the calibration file but was not found.
"
err_message
=
"
The field
'"
+
str
(
key
)
+
"'
is required within the calibration file but was not found.
"
err_message
+=
'
\n
'
+
str
(
append_to_error_message
)
if
append_to_error_message
else
''
err_message
+=
'
\n
'
+
str
(
append_to_error_message
)
if
append_to_error_message
else
''
return
dictionary_key_helper
(
dictionary
=
dictionary
,
key
=
key
,
throws
=
True
,
exception_msg
=
err_message
)
return
dictionary_key_helper
(
dictionary
=
dictionary
,
key
=
key
,
throws
=
True
,
case_insensitive
=
True
,
exception_msg
=
err_message
)
def
crop_banks_using_crop_list
(
bank_list
,
crop_values_list
):
def
crop_banks_using_crop_list
(
bank_list
,
crop_values_list
):
...
@@ -67,7 +70,7 @@ def crop_in_tof(ws_to_crop, x_min=None, x_max=None):
...
@@ -67,7 +70,7 @@ def crop_in_tof(ws_to_crop, x_min=None, x_max=None):
return
cropped_ws
return
cropped_ws
def
dictionary_key_helper
(
dictionary
,
key
,
throws
=
True
,
exception_msg
=
None
):
def
dictionary_key_helper
(
dictionary
,
key
,
throws
=
True
,
case_insensitive
=
False
,
exception_msg
=
None
):
"""
"""
Checks if the key is in the dictionary and performs various different actions if it is not depending on
Checks if the key is in the dictionary and performs various different actions if it is not depending on
the user parameters. If set to not throw it will return none. Otherwise it will throw a custom user message
the user parameters. If set to not throw it will return none. Otherwise it will throw a custom user message
...
@@ -75,12 +78,25 @@ def dictionary_key_helper(dictionary, key, throws=True, exception_msg=None):
...
@@ -75,12 +78,25 @@ def dictionary_key_helper(dictionary, key, throws=True, exception_msg=None):
:param dictionary: The dictionary to search for the key
:param dictionary: The dictionary to search for the key
:param key: The key to search for in the dictionary
:param key: The key to search for in the dictionary
:param throws: (Optional) Defaults to true, whether this should throw on a key not being present
:param throws: (Optional) Defaults to true, whether this should throw on a key not being present
:param case_insensitive (Optional) Defaults to false, if set to true it accounts for mixed case but is O(n) time
:param exception_msg: (Optional) The error message to print in the KeyError instead of the default Python message
:param exception_msg: (Optional) The error message to print in the KeyError instead of the default Python message
:return: The key if it was found, None if throws was set to false and the key was not found.
:return: The key if it was found, None if throws was set to false and the key was not found.
"""
"""
if
key
in
dictionary
:
if
key
in
dictionary
:
# Try to use hashing first
return
dictionary
[
key
]
return
dictionary
[
key
]
elif
not
throws
:
# If we still couldn't find it use the O(n) method
if
case_insensitive
:
# Convert key to str
lower_key
=
str
(
key
).
lower
()
for
dict_key
in
iterkeys
(
dictionary
):
if
str
(
dict_key
).
lower
()
==
lower_key
:
# Found it
return
dictionary
[
dict_key
]
# It doesn't exist at this point lets go into our error handling
if
not
throws
:
return
None
return
None
elif
exception_msg
:
elif
exception_msg
:
# Print user specified message
# Print user specified message
...
...
This diff is collapsed.
Click to expand it.
scripts/test/ISISPowderCommonTest.py
+
37
−
2
View file @
135e3480
...
@@ -13,7 +13,8 @@ class ISISPowderCommonTest(unittest.TestCase):
...
@@ -13,7 +13,8 @@ class ISISPowderCommonTest(unittest.TestCase):
def
test_cal_map_dict_helper
(
self
):
def
test_cal_map_dict_helper
(
self
):
missing_key_name
=
"
wrong_key
"
missing_key_name
=
"
wrong_key
"
correct_key_name
=
"
right_key
"
correct_key_name
=
"
right_key
"
dict_with_key
=
{
correct_key_name
:
123
}
expected_val
=
123
dict_with_key
=
{
correct_key_name
:
expected_val
}
# Check it correctly raises
# Check it correctly raises
with
assertRaisesRegex
(
self
,
KeyError
,
"
The field
'"
+
missing_key_name
+
"'
is required
"
):
with
assertRaisesRegex
(
self
,
KeyError
,
"
The field
'"
+
missing_key_name
+
"'
is required
"
):
...
@@ -26,7 +27,19 @@ class ISISPowderCommonTest(unittest.TestCase):
...
@@ -26,7 +27,19 @@ class ISISPowderCommonTest(unittest.TestCase):
append_to_error_message
=
appended_e_msg
)
append_to_error_message
=
appended_e_msg
)
# Check that it correctly returns the key value where it exists
# Check that it correctly returns the key value where it exists
self
.
assertEqual
(
common
.
cal_map_dictionary_key_helper
(
dictionary
=
dict_with_key
,
key
=
correct_key_name
),
123
)
self
.
assertEqual
(
common
.
cal_map_dictionary_key_helper
(
dictionary
=
dict_with_key
,
key
=
correct_key_name
),
expected_val
)
# Check it is not case sensitive
different_case_name
=
"
tEsT_key
"
dict_with_mixed_key
=
{
different_case_name
:
expected_val
}
try
:
self
.
assertEqual
(
common
.
cal_map_dictionary_key_helper
(
dictionary
=
dict_with_mixed_key
,
key
=
different_case_name
.
lower
()),
expected_val
)
except
KeyError
:
# It tried to use the key without accounting for the case difference
self
.
fail
(
"
cal_map_dictionary_key_helper attempted to use a key without accounting for case
"
)
def
test_crop_banks_using_crop_list
(
self
):
def
test_crop_banks_using_crop_list
(
self
):
bank_list
=
[]
bank_list
=
[]
...
@@ -123,6 +136,28 @@ class ISISPowderCommonTest(unittest.TestCase):
...
@@ -123,6 +136,28 @@ class ISISPowderCommonTest(unittest.TestCase):
self
.
assertEqual
(
common
.
dictionary_key_helper
(
dictionary
=
test_dictionary
,
key
=
good_key_name
),
123
)
self
.
assertEqual
(
common
.
dictionary_key_helper
(
dictionary
=
test_dictionary
,
key
=
good_key_name
),
123
)
def
test_dictionary_key_helper_handles_mixed_case
(
self
):
mixed_case_name
=
"
tEsT_KeY
"
lower_case_name
=
mixed_case_name
.
lower
()
expected_val
=
456
mixed_case_dict
=
{
mixed_case_name
:
expected_val
}
# Check by default it doesn't try to account for key
with
self
.
assertRaises
(
KeyError
):
common
.
dictionary_key_helper
(
dictionary
=
mixed_case_dict
,
key
=
lower_case_name
)
# Next check if we have the flag set to False it still throws
with
self
.
assertRaises
(
KeyError
):
common
.
dictionary_key_helper
(
dictionary
=
mixed_case_dict
,
key
=
lower_case_name
,
case_insensitive
=
False
)
# Check we actually get the key when we do ask for case insensitive checks
try
:
val
=
common
.
dictionary_key_helper
(
dictionary
=
mixed_case_dict
,
key
=
lower_case_name
,
case_insensitive
=
True
)
self
.
assertEqual
(
val
,
expected_val
)
except
KeyError
:
self
.
fail
(
"
dictionary_key_helper did not perform case insensitive lookup
"
)
def
test_extract_ws_spectra
(
self
):
def
test_extract_ws_spectra
(
self
):
number_of_expected_banks
=
5
number_of_expected_banks
=
5
ws_to_split
=
mantid
.
CreateSampleWorkspace
(
XMin
=
0
,
XMax
=
1
,
BankPixelWidth
=
1
,
ws_to_split
=
mantid
.
CreateSampleWorkspace
(
XMin
=
0
,
XMax
=
1
,
BankPixelWidth
=
1
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment