Skip to content
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

Open
thepabloaguilar opened this issue Jan 8, 2021 · 3 comments
Open

Implements _.method() #46

thepabloaguilar opened this issue Jan 8, 2021 · 3 comments

Comments

@thepabloaguilar
Copy link
Member

thepabloaguilar commented Jan 8, 2021

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!

@internetimagery
Copy link

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

@gtors
Copy link

gtors commented Oct 3, 2021

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    ")

@PrVrSs
Copy link

PrVrSs commented Jun 15, 2023

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'}}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants