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

fixed `source_to_function`

parent dfc1fc2c
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
* updated docs for `Loadable.refresh` * updated docs for `Loadable.refresh`
* added `DefaultDict` * added `DefaultDict`
* fixed `source_to_function`
__version__ = '2.14.20_a3' __version__ = '2.14.20'
...@@ -113,12 +113,12 @@ def source_to_function(src: tp.Union[tp.Callable, str]) -> tp.Callable[[tp.Any], ...@@ -113,12 +113,12 @@ def source_to_function(src: tp.Union[tp.Callable, str]) -> tp.Callable[[tp.Any],
:param src: a callable or a Python string expression :param src: a callable or a Python string expression
:return: a callable :return: a callable
""" """
if callable(src): if isinstance(src, str):
return src
else:
q = dict(globals()) q = dict(globals())
exec('_precond = lambda x: ' + src, q) exec('_precond = lambda x: ' + src, q)
return q['_precond'] return q['_precond']
else:
return src
def update_attr_if_none(obj: object, attr: str, value: tp.Any, def update_attr_if_none(obj: object, attr: str, value: tp.Any,
......
import unittest import unittest
from satella.coding import source_to_function
from satella.coding.predicates import x, build_structure from satella.coding.predicates import x, build_structure
class TestPredicates(unittest.TestCase): class TestPredicates(unittest.TestCase):
def test_source_to_function(self):
y = source_to_function(x)
self.assertEqual(y(5), 5)
def test_build_structure(self): def test_build_structure(self):
a = {x: x*2} a = {x: x*2}
b = [x, x*2, x*3] b = [x, x*2, x*3]
......
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