-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements _.method()
#46
Comments
One possible concept is to pull the bytecode of the calling frame, then walking through it following the stack changes, to get all calls within the "scope" of the lambdas usage. eg https://gist.github.com/internetimagery/05082fac28bc17860ec23fa0d7172df7 |
Another alternative: # pip install lambdaz
from lambdaz import a0
# a0 is the first argument with index 0
# `_` at the end is used as a termination sign. After accessing this attribute, the expression becomes callable
fn = a0.strip().title()[::-1]._
fn(" drow lleh ") |
Some ideas: def __getattr__(self, item: str):
def wrapper(*args, **kwargs):
return operator.methodcaller(item, *args, **kwargs)
return wrapper If need support chain of fn call such as class _Callable(object):
def __getattr__(self, item: str):
if item == '__call_chain__':
return object.__getattribute__(self, item)
def wrapper(*args, **kwargs):
_ = _Callable()
setattr(
_,
'__call_chain__',
[
*getattr(self, '__call_chain__', []),
operator.methodcaller(item, *args, **kwargs)
]
)
return _
return wrapper
def __call__(self, obj):
for fn in getattr(self, '__call_chain__', []):
obj = fn(obj)
return obj double_get = _.get('first_key').get('second_key')
print(double_get({'first_key': {'second_key': 'result'}})) |
I'm opening this issue to discuss the possible ways to implement
_.method()
Currently, we have this implementation:
The problem here is that we return the operator directly:
My idea is to create an intermediated class like
_MathExpression
:This implementation has a limitation, they won't work correctly if we try to call a method with one argument. This is hard because we don't know when the attribute is callable!
The text was updated successfully, but these errors were encountered: