Dataclasses reinvented, dependency injection, Python 3.7+
Project description
Class With auto-init
from auto_init import auto_init_class
@auto_init_class
class Point:
x: int
y: int
p = Point(y=-5)
assert p.x == 0
assert p.y == -5
assert str(p) == '<Point x=0, y=-5>'
You Can Still Have Your __init__
@auto_init_class
class Point:
x: int
y: int
def __init__(self, *args):
if args:
self.x, self.y = args
Singletons
@auto_init_class(singleton=True)
class AppModel:
pass
@auto_init_class
class AppPresenter:
model: AppModel
@auto_init_class
class AppView:
model: AppModel
@auto_init_class
class App:
model: AppModel
view: AppView
presenter: AppPresenter
app = App()
assert isinstance(app.view.model, AppModel)
assert app.view.model is app.presenter.model
Access to the Base Class
@auto_init_class
class Point:
x: int
y: int
primitive_point = Point(auto_init_base=True)
initialised_point = Point(x=10)
assert isinstance(primitive_point, Point._auto_init_base)
assert not hasattr(primitive_point, 'x')
assert isinstance(initialised_point, Point)
assert initialised_point.x == 10
Context
Context allows setting custom providers.
from auto_init import AutoInitContext, auto_init_class
@auto_init_class
class Line:
start: Point
end: Point
context = AutoInitContext(providers={Point: Point3d})
with context:
assert isinstance(Point(), Point3d)
assert isinstance(Line().start, Point3d)
Also, singletons apply only to the current context.
Inheritance Works
@auto_init_class
class Point:
x: int
y: int
@auto_init_class
class Point3d(Point):
z: int
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
auto-init-0.0.1.tar.gz
(4.3 kB
view details)
Built Distribution
File details
Details for the file auto-init-0.0.1.tar.gz
.
File metadata
- Download URL: auto-init-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 743bcc3e7555545a9c52b61e5c64ad9502665c650fd9632c3c35e49fedd1f315 |
|
MD5 | 6711e5b5b01496012a47f44dc01e75db |
|
BLAKE2b-256 | d2cc00e089fed8cb11e6a2132f4b34d9a101502b8f573381a4fc94bdb5677c6b |
File details
Details for the file auto_init-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: auto_init-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c153fa0578d4f99dd0bbf0f2ed67bbeaf75a5f9e7e118f00d6b49c52be3c3c1 |
|
MD5 | e341c1694134ea369f9fbaa716c902a6 |
|
BLAKE2b-256 | 24917dec48f92d942651bf93969b63d800b778955f214d1a20ef120815f21f52 |