Skip to main content

Simple-PyDI

Intro

simple python dependency injection framwork

just like java spring-framework.

Install

pip install simple-pydi

Usage

Components

app.py

class Engine:
    pass


class Wheels:
    def __init__(self, wheels_count: int = 4):
        self.wheels_count = wheels_count


class Body:
    pass


class Person:
    def __init__(self, name: str):
        self.name = name


class Car:
    def __init__(self, engine: Engine, wheels: Wheels, body: Body, driver: Person):
        self.engine = engine
        self.wheels = wheels
        self.body = body
        self.driver = driver

Direct Register

main.py

from di import NewContext

from app import Car, Engine, Wheels, Body, Person


ctx = NewContext()

# directly register beans
ctx.register(Car, id='car')
ctx.register(Engine)
ctx.register(Wheels)
ctx.register(Body)
ctx.register(Person, consts={'name': 'Ben'})

car: Car = ctx.instance_by_id('car')

assert car.driver.name == 'Ben'
assert car.wheels.wheels_count == 4

# view beans xml definition
print(ctx.format_beans_xml())

Register by XML File

beans.xml

<?xml version="1.0" ?>
<beans>
	<bean cls="app.Car" id="car" singleton="true">
		<property name="engine" ref="cls:app.Engine"/>
		<property name="wheels" ref="cls:app.Wheels"/>
		<property name="body" ref="cls:app.Body"/>
		<property name="driver" ref="cls:app.Person"/>
	</bean>
	<bean cls="app.Engine" id="app.Engine" singleton="true"/>
	<bean cls="app.Wheels" id="app.Wheels" singleton="true">
		<property name="wheels_count" value="4" value-type="int"/>
	</bean>
	<bean cls="app.Body" id="app.Body" singleton="true"/>
	<bean cls="app.Person" id="app.Person" singleton="true">
		<property name="name" value="Ben" value-type="str"/>
	</bean>
</beans>

main.py

from di import NewContext

from app import Car


ctx = NewContext()

# get beans definition from xml file
ctx.register_file('beans.xml')

car: Car = ctx.instance_by_id('car')

assert car.driver.name == 'Ben'
assert car.wheels.wheels_count == 4

# view beans xml definition
print(ctx.format_beans_xml())

Use Decorator Definition

app.py

from di.decorator import bean


@bean
class Engine:
    pass


@bean
class Wheels:
    def __init__(self, wheels_count: int = 4):
        self.wheels_count = wheels_count


@bean
class Body:
    pass


@bean(consts={'name': 'Ben'})
class Person:
    def __init__(self, name: str):
        self.name = name


@bean(id='car')
class Car:
    def __init__(self, engine: Engine, wheels: Wheels, body: Body, driver: Person):
        self.engine = engine
        self.wheels = wheels
        self.body = body
        self.driver = driver

main.py

from di import NewContext

from app import Car

ctx = NewContext()

# register pre defined group (by decorator) 
ctx.register_group()

car: Car = ctx.instance_by_cls(Car)

assert car.driver.name == 'Ben'
assert car.wheels.wheels_count == 4

# view beans xml definition
print(ctx.format_beans_xml())

Release files for Simple-PyDI 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Simple-PyDI 0.9.0
File Size Uploaded
Simple-PyDI-0.9.0.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Simple-PyDI 0.9.0
File Interpreter ABI Platform
Simple_PyDI-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size:13.7 kB

Release files / Simple-PyDI-0.9.0.tar.gz

Download URL Simple-PyDI-0.9.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f871ca0201d81c7f0908f59703c9da69fcf823273c636f98e7ac779abd3f16c8
BLAKE2b-256 checksum
How to use checksums
32bd1268108b4d67e4bb9c5e6d4bef46e7a13328974d0cdda8950ac72202d725
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release files / Simple_PyDI-0.9.0-py3-none-any.whl

Download URL Simple_PyDI-0.9.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61bd0ac4b5109e97f5da4c8f606ab6477fb309b7ebcb2f43658be59becf3a0ea
BLAKE2b-256 checksum
How to use checksums
35da6e63363636f81640040543fdff5e467f5f18b7c36f344e2f9e6605608250
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page