Skip to main content

acetone library

About

Glue code removal. Acetone is a python library to provide inversion of control in situation where other methods are inconvenient or they are not even possible. Or you just like the library.

Usage

Create the acetone container somewhere in your application:

# dependencies.py

from acetone import AcetoneContainer

dependencies = AcetoneContainer()
# or ioc_container
# or lord_of_the_dependencies
# or services

Then use it:

# class_with_dependency.py

from dependencies import dependencies

class ClassWithSomeDependency(object):
    # you can use strings or types as a key
    dependency = dependencies.Dependency('key')

    def use_the_dependency(self):
        self.dependency.dependency_call('argument')

Create a dependency implementation:

# dependency_implementation.py

class DependencyImplementation(object):
    def dependency_call(self, argument):
        print(argument)

Later register the implementation and run it!

# __main__.py

from dependencies import dependencies
from class_with_dependency import ClassWithSomeDependency
from dependency_implementation import DependencyImplementation


if __name__ == '__main__':
    dependency_implementation = DependencyImplementation()
    dependencies.register_instance('key', dependency_implementation)

    instance = ClassWithSomeDependency()
    instance.use_the_dependency()

Or load it from a file:

[
    {
        "name": "key",
        "module": "dependency_implementation",
        "factory": "DependencyImplementation",
        "singleton": true
    }
]
import json
from dependencies import dependencies

def main():
    with open('configuration.json') as file:
        content = json.load(file)
        dependencies.load_from_dicts(content)

    instance = ClassWithSomeDependency()
    instance.use_the_dependency()

Frequently asked questions

How fast is it?

It’s very fast. It’s even faster then a builtin property. The very first dependency access requires some initialization for its own setup and dependency creation (provided it was not created before), but the subsequent calls are as fast as a member instance access. Dependencies use a descriptor protocol (used by @property), they are initialized lazily and once fetched from the container they are set as a normal instance member (class member in case of ClassDependency). This trick is used by several frameworks (for example werkzeuq cached_property).

How do I mock it?

Technically you can mock it, but I don’t think it’s necessary. The container is simple and well tested. Its purpose is to provide a requested dependency and the dependency can be a mock as well. You can just consider it as an essential part of your code and not mock it to your advantage (would you mock properties?).

class TestXyz(TestCase):
    def tearDown(self):
        container.clean()

Traditionalists wouldn’t agree for sure but Python wasn’t created by traditionalists in the first place.

Are there any requirements?

No external dependencies. For the class used the only requirement is that the class has to be a normal python class with __dict__. In other words it can’t use __slots__.

Metadata

Release files for acetone 0.1.0

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

Built distribution (wheel)

Table of built distributions (wheels) for acetone 0.1.0
File Interpreter ABI Platform Actions
acetone-0.1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for acetone-0.1.0-py2.py3-none-any.whl

Release files / acetone-0.1.0-py2.py3-none-any.whl

Download URL acetone-0.1.0-py2.py3-none-any.whl
Size 10.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d297e54cec80df29e790cc8741055c995da93b8e534077554256948edbae37bf
BLAKE2b-256 checksum
How to use checksums
6ea2f93f818f18b713f6897de0edc405b84048c17e9ef9ebcea01990066cf833
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for acetone 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file No provenance

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