3 ms·
Cool, any examples of libraries that use it for dependency injection or FFI or validation?
by arjvik 4y ago
Cool, any examples of libraries that use it for dependency injection or FFI or validation?
- GalaxySnail 4y agoFor FFI, there is a pure python mode in cython: https://cython.readthedocs.io/en/latest/src/tutorial/external.html
- BiteCode_dev 4y agodjango-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.
- kylebarron 4y agoSpecifically it's the underlying pydantic that does validation based on annotations https://pydantic-docs.helpmanual.io/ https://pydantic-docs.helpmanual.io/
- BiteCode_dev 4y agoIndeed. 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 https://github.com/adriangb/di
- LtWorf 4y agoI 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
- kortex 4y agoDependency-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/di_in_python.html https://python-dependency-injector.ets-labs.org/introduction...
- chlorion 4y agoIt'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 https://docs.python.org/3/library/dataclasses.html