terra.utils
1# Copyright 2022 Terra Enabling Developers Limited 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import typing as t 15 16 17def update_if_not_none(to_update: t.Dict[str, t.Any], new_values: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: 18 """ 19 Insert all values from ``new_values`` into ``to_update``, overwriting the values 20 for any keys already present, unless the value is ``None``, in which case do nothing. 21 22 Args: 23 to_update (:obj:`dict`): dict object to be updated with additional values 24 new_values (:obj:`dict`): dict object to be updated with additional values 25 26 Returns: 27 :obj:`dict`: to_update object updated with all values in new_values object which were not None 28 """ 29 for k, v in new_values.items(): 30 if v is not None: 31 to_update[k] = v 32 return to_update
def
update_if_not_none(to_update: Dict[str, Any], new_values: Dict[str, Any]) -> Dict[str, Any]:
18def update_if_not_none(to_update: t.Dict[str, t.Any], new_values: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: 19 """ 20 Insert all values from ``new_values`` into ``to_update``, overwriting the values 21 for any keys already present, unless the value is ``None``, in which case do nothing. 22 23 Args: 24 to_update (:obj:`dict`): dict object to be updated with additional values 25 new_values (:obj:`dict`): dict object to be updated with additional values 26 27 Returns: 28 :obj:`dict`: to_update object updated with all values in new_values object which were not None 29 """ 30 for k, v in new_values.items(): 31 if v is not None: 32 to_update[k] = v 33 return to_update
Insert all values from new_values
into to_update
, overwriting the values
for any keys already present, unless the value is None
, in which case do nothing.
Args:
to_update (dict
): dict object to be updated with additional values
new_values (dict
): dict object to be updated with additional values
Returns:
dict
: to_update object updated with all values in new_values object which were not None