import module within python lambda function - python

Is it possible to import module within a python lambda function? For example, i have a lambda function which requires to import math
import math
is_na_yes_no = lambda x: 'Yes' if math.isnan(x) else 'No'
How can I include the import math statement within the lambda function?
To clarify, I have a scenario that need to put some lambda functions in a config file and evaluate the function in some other python files, exmample:
{
"is_na_yes_no" = "lambda x: 'Yes' if math.isnan(x) else 'No'"
}
In this case, the python file that evaluating those lambda functions need to all modules required.

Thanks #L3viathan for the answer.
Here is same thing without having to import math in the module.
is_na_yes_no = lambda x: 'Yes' if __import__('math').isnan(x) else 'No'
It highlights the flexibility of python -- __import__ feature can be used in lambda functions instead of having them written out before hand.

Related

Weird Python Lambda() syntax

As below, I understand lambda y:... .
But the first Lambda(...) is a function?.
ds = datasets.FashionMNIST(
...
target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)
It's just a function in torchvision for wrapping an arbitrary function as a transform. It's nothing to do with Python syntax, and is spelled Lambda with a capital L instead of lambda to not conflict with the Python keyword.

Using quad(str..) to integrate

Hello I'm trying to integrate a function stored in a variable
from scipy.integrate import quad
fun= x+1
result= quad(fun,2,0)
I know the fun should be instead a int but do you know how I can integrate a function stored this way? I tried to use lambda x:... but I want to use 'fun' and not lambda x :x+1

Using Sympy on methods from the math module

As part of a wider plotting program I have a script where there is a function f defined as a lambda expression, and I need to find its derivative. My code for testing the script is:
import math
import sympy as sym
f = lambda x : math.sin(x)
sx = sym.symbols('x')
sf = sym.diff(f(sx), sx)
print(sf)
While this works fine when f is defined without using any outside methods, the above throws TypeError: can't convert expression to float because the math module's methods don't support the symbolic representations used by Sympy. However, while this works when f instead uses sym.sin, because of how the aforementioned program is structured the definition of f cannot be changed. Is there a way to symbolically interpret the math module's methods?

How do you make lambda a symbol when importing all Sympy functions?

If I import sympy with:
from sympy import *
Then how do set lambda to be a symbol and not a function?
E.g.
lambda = symbols('lambda')
In my case, likely because sympy has all its functions imported (I am working in a sympy only environment so it is convenient), I receive the error:
lambda = symbols('lambda')
^
SyntaxError: invalid syntax
Is there any way to avoid this if I am importing all the functions from sympy?
Thank you
This is because lambda is a keyword for creating lambda functions. You can't use it as your variable name. You'll have to find a new name. Even if you find a way to assign lambda to something, it won't work because it's not parsed as a potential variable name.
For example:
lambda_ = symbols('lambda')
will not have the same error.

Using (as of now) Undefined Functions in Lambda Functions in Python

I would like to define a lambda function in a different module than it will be executed in. In the module that the lambda will be called, there are methods available that aren't when the lambda is defined. As it is, Python throws an error when the lambda tries to employ those functions.
For example, I have two modules.
lambdaSource.py:
def getLambda():
return lambda x: squareMe(x)
runMe.py
import lambdaSource
def squareMe(x):
return x**2
if __name__ == '__main__':
theLambdaFunc = lambdaSource.getLambda()
result = theLambdaFunc(5)
If you run runMe.py, you get a Name Error: NameError: global name 'squareMe' is not defined
The only way I can get around this is to modify the lambda's global variables dictionary at runtime.
theLambdaFunc.func_globals['squareMe'] = squareMe
This example is contrived, but this is the behavior I desire. Can anyone explain why the first example doesn't work? Why 'squareMe' isn't available to the scope of the lambda function? Especially when, if I just defined the lambda below the function squareMe, everything works out okay?
You're defining getLambda and squareMe in separate modules. The lambdaSource module only sees what's defined in its scope -- that is, everything you define directly in it and everything you import in it.
To use squareMe from getLambda, you need lambdaSource.py to have a from runMe import squareMe statement (and not the other way around as you seem to be doing).

Categories