Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Cool, any examples of libraries that use it for dependency injection or FFI or validation?


Dependency-injector uses it for exactly this purpose

    @inject
    def main(service: Service = Provide[Container.service]) -> None:
        ...

FastAPI also does a similar sort of trick, for DI and also request parsing.

https://python-dependency-injector.ets-labs.org/introduction...


django-ninja and fastapi does dep injection, parsing and validation based on annotations.

taichi does ffi with their own dsl, cython with c.

I you haven't tried pydantic or sister projects (django-ninja, fastapi and typer), this is a good place to start. They bring a breath of fresh air and are quite fun to use.


Specifically it's the underlying pydantic that does validation based on annotations

https://pydantic-docs.helpmanual.io/


Indeed.

The dep injection is fastapi and typer code though, and quite tied to it. So it's worth mentioning that somebody is attempting (quite successfully from the look of it) to create a generic lib of dep injection using annotation named DI, inspired by those libs: https://github.com/adriangb/di


I always find it very funny that fastapi has that name when it relies on pydantic that is one of the slowest libraries in that space :D


It's not one of those that you listed, but the @dataclass decorator uses type hints to auto-generate classes and class constructors (__init__ method) for you.

An example:

    @dataclass
    class HackerNewsPost:
        title: str
        author: str
        votes: int = 0
This will generate a constructor that looks like this:

    def __init__(title: str, author: str, votes: int = 0):
        self.title = title
        self.author = author
        self.votes = votes
This is pretty useful, writing __init__ boilerplate has been a common thing in my experience using Python, and it makes code more compact and readable.

It can do some other things too!

https://docs.python.org/3/library/dataclasses.html


For FFI, there is a pure python mode in cython:

  https://cython.readthedocs.io/en/latest/src/tutorial/external.html




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: