Skip to content
Snippets Groups Projects
Commit 07a1d288 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added update_key_if_true, v2.11.26

parent ec69eca6
No related branches found
Tags v2.11.26
No related merge requests found
......@@ -12,3 +12,4 @@
* deprecated `count(start_at=)`
* renamed the argument in `half_cartesian`
* minor typing, refactors and docs changes
* added `update_key_if_true`
......@@ -10,6 +10,8 @@ Functions and decorators
.. autofunction:: satella.coding.update_key_if_not_none
.. autofunction:: satella.coding.update_key_if_true
.. autofunction:: satella.coding.update_attr_if_none
.. autofunction:: satella.coding.merge_dicts
......
__version__ = '2.11.26_a11'
__version__ = '2.11.26'
......@@ -13,7 +13,7 @@ from .iterators import hint_with_length, SelfClosingGenerator, exhaust, chain
from .metaclasses import metaclass_maker, wrap_with, dont_wrap, wrap_property, DocsFromParent, \
CopyDocsFrom
from .misc import update_if_not_none, update_key_if_none, update_attr_if_none, queue_iterator, \
update_key_if_not_none, source_to_function
update_key_if_not_none, source_to_function, update_key_if_true
from .recast_exceptions import rethrow_as, silence_excs, catch_exception, log_exceptions, \
raises_exception
from .overloading import overload, class_or_instancemethod
......@@ -22,6 +22,7 @@ __all__ = [
'overload', 'class_or_instancemethod',
'update_if_not_none', 'DocsFromParent', 'update_key_if_none', 'queue_iterator',
'update_attr_if_none', 'update_key_if_not_none', 'source_to_function',
'update_key_if_true',
'hint_with_length', 'SelfClosingGenerator', 'exhaust', 'chain',
'Monitor', 'RMonitor', 'merge_dicts',
'short_none', 'has_keys', 'CopyDocsFrom',
......
......@@ -67,6 +67,21 @@ def update_attr_if_none(obj: object, attr: str, value: tp.Any,
raise
def update_key_if_true(dictionary: dict, key: tp.Any, value: tp.Any, flag: bool):
"""
If flag is True, execute dictionary[key] = value
:param dictionary: dictionary to mutate
:param key: dictionary key to use
:param value: dictionary value to set
:param flag: whether to execute the setting operation
:return: the dict itself
"""
if flag:
dictionary[key] = value
return dictionary
def update_key_if_none(dictionary: dict, key, value):
"""
This is deprecated. Please use update_key_if_not_none instead!
......
from satella.coding import update_key_if_not_none, overload, class_or_instancemethod
from satella.coding import update_key_if_not_none, overload, class_or_instancemethod, \
update_key_if_true
import unittest
class TestCase(unittest.TestCase):
def test_update_key_if_true(self):
a = {5: False}
update_key_if_true(a, 5, True, False)
self.assertFalse(a[5])
update_key_if_true(a, 5, True, True)
self.assertTrue(a[5])
def test_class_or_instancemethod(self):
a = {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment