Skip to content

Commit 9e6f9b9

Browse files
author
Sylvain MARIE
committed
Two helper functions is_mini_lambda_expr and as_function. Fixes #13
1 parent ed4e968 commit 9e6f9b9

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

mini_lambda/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from mini_lambda.base import FunctionDefinitionError, evaluate, get_repr
22

33
# this one only exports one private class, no need
4-
# from mini_lambda.generated import *
4+
# from mini_lambda.generated_magic import *
55

66
from mini_lambda.main import _, L, F, C, Not, And, Or, Format, Get, In, Slice, InputVar, Constant, \
7-
make_lambda_friendly, make_lambda_friendly_method, make_lambda_friendly_class
7+
make_lambda_friendly, make_lambda_friendly_method, make_lambda_friendly_class, as_function, is_mini_lambda_expr
88

99
__all__ = [
1010
# submodules
11-
'base', 'generated_magic', 'generated_magic_replacements', 'main', 'symbols', 'vars',
11+
'base', 'generated_magic_replacements', 'main', 'symbols', 'vars', # generated_magic
1212
# symbols
1313
'FunctionDefinitionError', 'evaluate', 'get_repr',
1414
'_', 'L', 'F', 'C', 'InputVar', 'Constant', 'make_lambda_friendly', 'make_lambda_friendly_method',
15-
'make_lambda_friendly_class',
15+
'make_lambda_friendly_class', 'as_function', 'is_mini_lambda_expr',
1616
'Not', 'And', 'Or', 'Format', 'Get', 'In', 'Slice',
1717
]
1818

mini_lambda/main.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import sys
33

44
try: # python 3.5+
5-
from typing import TypeVar, Union, Tuple, Callable, Type
5+
from typing import TypeVar, Union, Tuple, Callable, Any
6+
try: # old python 3.5
7+
from typing import Type
8+
except ImportError:
9+
pass
610
T = TypeVar('T')
711
except ImportError:
812
pass
@@ -705,3 +709,31 @@ def bar2(cls, times, num, den):
705709
:return:
706710
"""
707711
return Constant(method, name)
712+
713+
714+
def is_mini_lambda_expr(f # type: Union[LambdaExpression, Any]
715+
):
716+
"""
717+
Returns `True` if f is a mini-lambda expression, False otherwise
718+
719+
:param f:
720+
:return:
721+
"""
722+
try:
723+
return issubclass(f.__class__, LambdaExpression)
724+
except (AttributeError, TypeError):
725+
return False
726+
727+
728+
def as_function(f # type: Union[LambdaExpression, Any]
729+
):
730+
"""
731+
Returns `f.as_function()` if f is a mini-lambda expression, returns f otherwise.
732+
733+
:param f:
734+
:return:
735+
"""
736+
if is_mini_lambda_expr(f):
737+
return f.as_function()
738+
else:
739+
return f

mini_lambda/tests/test_mini_lambda.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
from mini_lambda import InputVar, Len, Str, Int, Repr, Bytes, Sizeof, Hash, Bool, Complex, Float, Oct, Iter, \
5-
Any, All, _, Slice, Get, Not, FunctionDefinitionError, Format, C, And, Or, Round
5+
Any, All, _, Slice, Get, Not, FunctionDefinitionError, Format, C, And, Or, Round, as_function, is_mini_lambda_expr
66
from math import cos
77
from numbers import Real
88

@@ -799,3 +799,38 @@ def test_constants_methods_can_be_combined():
799799

800800
f = _(a(0) + a(0))
801801
assert f(None) == 2.
802+
803+
804+
def test_is_mini_lambda_expr():
805+
"""Tests that `is_mini_lambda_expr` works"""
806+
807+
from mini_lambda import x
808+
809+
# mini lambda: true
810+
assert is_mini_lambda_expr(x ** 2)
811+
812+
# standard lambda: false
813+
assert not is_mini_lambda_expr(lambda x: x)
814+
815+
# standard function: false
816+
def foo():
817+
pass
818+
assert not is_mini_lambda_expr(foo)
819+
820+
# mini lambda as function: false
821+
f = as_function(x ** 2)
822+
assert not is_mini_lambda_expr(f)
823+
824+
825+
def test_as_function():
826+
"""Tests that `as_function` works"""
827+
828+
# it transforms mini-lambda exprs...
829+
from mini_lambda import x
830+
f = as_function(x ** 2)
831+
assert f(2) == 4
832+
833+
# but supports normal functions too
834+
def foo(x):
835+
pass
836+
assert as_function(foo) is foo

0 commit comments

Comments
 (0)