Skip to content

Commit

Permalink
Added dict_combine function for Python 2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerkin committed May 8, 2019
1 parent 6a144e8 commit cd312c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sciunit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .validators import ObservationValidator, ParametersValidator
from .errors import Error, CapabilityError, ObservationError,\
InvalidScoreError, ParametersError
from .utils import dict_combine


class Test(SciUnit):
Expand All @@ -32,7 +33,7 @@ def __init__(self, observation, name=None, **params):

# Use a combination of default_params and params, choosing the latter
# if there is a conflict.
self.params = {**self.default_params, **params}
self.params = dict_combine(self.default_params, params)
self.verbose = self.params.pop('verbose', 1)
self.validate_params(self.params)
# Compute possible new params from existing params
Expand Down
10 changes: 10 additions & 0 deletions sciunit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ def set_warnings_traceback(tb=True):
warnings.simplefilter("default")


def dict_combine(*dict_list):
"""Return the union of several dictionaries.
Uses the values from later dictionaries in the argument list when
duplicate keys are encountered.
In Python 3 this can simply be {**d1, **d2, ...}
but Python 2 does not support this dict unpacking syntax.
"""
return {k: v for d in dict_list for k, v in d.items()}


def rec_apply(func, n):
"""
Used to determine parent directory n levels up
Expand Down

0 comments on commit cd312c6

Please sign in to comment.