Skip to main content

Spring/Micronaut-esque dependency injection system for Python using type hints

Project description

Injection Mold

This module provides a Spring/Micronaut-esque dependency injection system for Python using type hints.

Example

from mold import factory, singleton, inject, BeanContext

class SqlSession:
    pass

class User:
    id: str

class IUserRepo(abc.ABC):
    @abc.abstractmethod
    def get_user_by_id(self, id: str) -> User:
        raise NotImplementedError()

@singleton()
class SqlUserRepo(IUserRepo):
    def __init__(self, session: SqlSession):
        self._session = session
        
    def get_user_by_id(self, id: str) -> User:
        # ...
        pass

@factory()
def sql_session_factory() -> SqlSession:
    return SqlSession()

@inject()
def get_user_service(user_repo: IUserRepo, id: str) -> User:
    return user_repo.get_user_by_id(id)

@app.route('/user/<id>')
def get_user_route(id: str):
    user = get_user_service(id)
    return jsonify(user) 

if __name__ == '__main__':
    with BeanContext():
        app.run()

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

injection-mold-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file injection-mold-0.0.1.tar.gz.

File metadata

  • Download URL: injection-mold-0.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for injection-mold-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0b17c706c3fb5703e29256b3dadc242f8797bde8472194976e20769de6f0fc30
MD5 e8088cacb77fef8ea20bca60f39fb8f2
BLAKE2b-256 116e5e446953588d272b3b07f4a78ccf73852f16fce457f0ec2c285c1d60fffe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page