dry-python / lambdas Goto Github PK
View Code? Open in Web Editor NEWWrite short and fully-typed lambdas where you need them.
License: BSD 2-Clause "Simplified" License
Write short and fully-typed lambdas where you need them.
License: BSD 2-Clause "Simplified" License
Hello! I suggest replacing manual Poetry installation to python-poetry-action.
Benefits from this approach:
Hello, I'm opening this issue to discuss about Math operations. I've had and initial idea!
Create an class, MathOperations
or something like, it'll store all operations to be executed when __call__
is called! So the math operations from _Callable
will return that class.
I've made a simple test without the class:
>>> import operation
>>> from lambdas import _fmap
>>> # _ + 1 - 10
>>> operations = [_fmap(operator.add)(None, 1), _fmap(operator.sub)(None, 10)]
>>> first_operation, *rest_operations = operations
>>> reduce(lambda partial_result, operation: operation(partial_result), rest_operations, first_operation(1))
-8
I was worried about the operations order, but Python does it for us!
>>> # If we have this `_ + 1 * 2`, the operations list should be like below
>>> operations = [_fmap(operator.add)(None, 3)]
Python deals with the operations order, we just have to store them.
It would be fine to setup documentation!
First of all, thanks for creating this library! It's quite neat and I really enjoy the idea of lambdas (I wrote things in scala and using lambdas for Python development feels more natural to me).
I have a quick question about using lambdas with returns.Maybe. Pardon me if I did something wrong, but I'm not sure how to use lambdas' _ in a bind function of a Maybe instance. Below is an example:
from typing import Optional
from returns.maybe import Maybe, Nothing
from lambdas import _
class Test:
a: Maybe[int]
b: Maybe[int]
def __init__(self, a: Optional[int] = None, b: Optional[int] = None) -> None:
self.a = Maybe.from_value(a)
self.b = Maybe.from_value(b)
def get_a(self) -> Maybe[int]:
return self.a
def get_b(self) -> Maybe[int]:
return self.b
def test_returns() -> None:
print("haha")
t = Test(13, None)
value = (
t.get_a()
.bind(Maybe.from_value(_ ** 2 if _ > 12 else Nothing)) // Break
.bind(Maybe.from_value(_ + 100)) // Break
)
print(value)
print(t.get_b().bind(lambda x: x + 12))
if __name__ == "__main__":
test_returns()
If I turn the two "Break" lines to using regular lambdas, the script executes as expected. Did I do something wrong here when using lambdas _ with Maybe.from_value?
Thanks!
Due to lack of __iter__
implementation, Python is attempting to iterate over _Callable
when possible. It's generally an undesired behavior, given the purposes of the _Callable
class.
You can easily reproduce the issue:
[*__import__("lambdas")._]
That should be a no-brainer to fix and write tests for.
class _Callable:
__iter__ = None
Your library seems to be very promising, but in my case I would benefit from it the most, if it would have __getattr__
implemented. When we can expect next release? As I can see, it is already implemented, but I guess there are some problems that stops you from releasing it?
Tried to create project with latest lambdas
and returns
to use them together.
pip
fails to install my dependencies with the following error:
The conflict is caused by:
returns 0.20.0 depends on typing-extensions<5.0 and >=4.0
lambdas 0.1.0 depends on typing-extensions<4.0 and >=3.7
Latest lambdas
should be installable together with latest returns
.
I tried to install lambdas from master
branch and everything resolves fine, because on master
we have typing-extensions = ">=4.0,<5.0"
. Can we just release new version from master
branch?
pyhon
version: 3.11.1lambdas
version: 0.1.0returns
version: 0.2.0When we define a class like this:
def func():
...
class MyClass:
my_func = lambda self, arg: arg
# or
class MyAnotherClass:
my_func = func
The coverage plugin will consider the my_func
as coveraged without any tests.
This behavior is expected because for it the my_func
is an attribute!
I think it will be great use the normal signature, this will enforce we to make dummy tests to ensures the _
will work correctly.
Is it possible to express this kind of lambda?
lambda a, b: a * b
I intuitively tried this approach, but it doesn't work...
_._1 * _._2
>>> complex_math_expression = 50 / (_ ** 2) * 2
>>> print(complex_math_expression(5))
100.0
But:
>>> 50 / (5 ** 2) * 2
4.0
Not good :)
Hello, could you publish a new release on pypi? I saw in the code that attribute access is supported, but the pypi release was made on the commit just before: c12d4ba so currently _.something
raises AttributeError
Dependabot can't resolve your Python dependency files.
As a result, Dependabot couldn't update your dependencies.
The error Dependabot encountered was:
Creating virtualenv lambdas-KN7S6Y4v-py3.9 in /home/dependabot/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies...
PackageNotFound
Package pytest-mypy-plugins (1.1.0) not found.
at /usr/local/.pyenv/versions/3.9.0/lib/python3.9/site-packages/poetry/repositories/pool.py:144 in package
140│ self._packages.append(package)
141│
142│ return package
143│
→ 144│ raise PackageNotFound("Package {} ({}) not found.".format(name, version))
145│
146│ def find_packages(
147│ self, dependency,
148│ ):
If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.
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!
Maybe it's a great idea change from Travis CI to GitHub Actions to everything be together on the same platform, like returns!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.