Implements `_.method()`
I'm opening this issue to discuss the possible ways to implement _.method()
Currently, we have this implementation:
class _Callable(object):
def __getattr__(
self,
key: str,
) -> Callable[[_LambdaDynamicProtocol[T1]], T1]:
return operator.attrgetter(key)
The problem here is that we return the operator directly:
>>> from lambdas import _
>>> _.method
operator.attrgetter('method')
>>> _.method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attrgetter expected 1 arguments, got 0
My idea is to create an intermediated class like _MathExpression:
class Attr: # Just ignore the name for now
def __init__(self, attr: str) -> None:
self._attrgetter = operator.attrgetter(attr)
def __call__(self, *args, **kwargs) -> Any:
if len(args) == 1:
return self._attrgetter(args[0])
return lambda obj: self._attrgetter(obj)(*args, **kwargs)
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!
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:
If need support only single call _.get('some_key') We can use closure with methodcaller.
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 _.get('a').get('b').
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'}}))