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

2.18.4

parent 22c50b53
No related branches found
No related tags found
No related merge requests found
# v2.18.4
* added `map_through` to `unpack_dict`
* added `execute_if_attribute_none`
* added `execute_if_attribute_not_none`
Decorators
==========
.. autofunction:: satella.coding.decorators.execute_if_attribute_none
.. autofunction:: satella.coding.decorators.execute_if_attribute_not_none
.. autofunction:: satella.coding.decorators.transform_result
.. autofunction:: satella.coding.decorators.transform_arguments
......
__version__ = '2.18.4a2'
__version__ = '2.18.4'
from .arguments import auto_adapt_to_methods, attach_arguments, for_argument, \
execute_before, copy_arguments, replace_argument_if, transform_result, \
transform_arguments
transform_arguments, execute_if_attribute_none, execute_if_attribute_not_none
from .decorators import wraps, chain_functions, has_keys, short_none, memoize, return_as_list, \
default_return, cache_memoize, call_method_on_exception
from .flow_control import loop_while, queue_get
......@@ -12,4 +12,5 @@ __all__ = ['retry', 'transform_result', 'transform_arguments',
'chain_functions', 'has_keys', 'short_none', 'auto_adapt_to_methods',
'attach_arguments', 'for_argument', 'loop_while', 'memoize',
'copy_arguments', 'replace_argument_if', 'return_as_list',
'default_return', 'cache_memoize', 'call_method_on_exception']
'default_return', 'cache_memoize', 'call_method_on_exception',
'execute_if_attribute_none', 'execute_if_attribute_not_none']
......@@ -373,3 +373,40 @@ def for_argument(*t_ops: ForArgumentArg, **t_kwops: ForArgumentArg):
return inner
return outer
def execute_if_attribute_none(attribute: str):
"""
Decorator for instancemethods.
This will execute the function only if provided attribute is None.
Otherwise it will return a None
:param attribute: name of the attribute to check
"""
def outer(fun):
@wraps(fun)
def inner(self, *args, **kwargs):
if getattr(self, attribute) is None:
return fun(self, *args, **kwargs)
return inner
return outer
def execute_if_attribute_not_none(attribute: str):
"""
Decorator for instancemethods.
This will execute the function only if provided attribute is not None.
Otherwise it will return a None
:param attribute: name of the attribute to check
"""
def outer(fun):
@wraps(fun)
def inner(self, *args, **kwargs):
if getattr(self, attribute) is not None:
return fun(self, *args, **kwargs)
return inner
return outer
......@@ -9,7 +9,8 @@ from satella.coding import wraps, chain_functions, postcondition, \
from satella.coding.decorators import auto_adapt_to_methods, attach_arguments, \
execute_before, loop_while, memoize, copy_arguments, replace_argument_if, \
retry, return_as_list, default_return, transform_result, transform_arguments, \
cache_memoize, call_method_on_exception
cache_memoize, call_method_on_exception, execute_if_attribute_none, \
execute_if_attribute_not_none
from satella.coding.predicates import x
from satella.exceptions import PreconditionError
......@@ -18,6 +19,42 @@ logger = logging.getLogger(__name__)
class TestDecorators(unittest.TestCase):
def test_execute_if_attribute_not_none(self):
class ExecIfAttrNone:
def __init__(self):
self.attr = None
self.called = 0
@execute_if_attribute_not_none('attr')
def run_me(self):
self.called += 1
e = ExecIfAttrNone()
self.assertEqual(e.called, 0)
e.run_me()
self.assertEqual(e.called, 0)
e.attr = 2
e.run_me()
self.assertEqual(e.called, 1)
def test_execute_if_attribute_none(self):
class ExecIfAttrNone:
def __init__(self):
self.attr = None
self.called = 0
@execute_if_attribute_none('attr')
def run_me(self):
self.called += 1
e = ExecIfAttrNone()
self.assertEqual(e.called, 0)
e.run_me()
self.assertEqual(e.called, 1)
e.attr = 2
e.run_me()
self.assertEqual(e.called, 1)
def test_call_method_on_exception(self):
a = {'called': False}
slf = self
......
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