Skip to main content

A simple mixin system with method exports

Project description

Mixit

A lightweight and flexible mixin system for Python that allows dynamic composition of functionality through mixins.

Features

  • Dynamic mixin composition at runtime
  • Method export system with conflict detection
  • Optional initialization through mix_init
  • Clean mixin coordination through mixer access
  • Simple and intuitive API

Installation

pip install git+https://github.com/joshms123/mixit.git

Quick Start

from mixit import Mixer, Mixin, export

# Define a mixin
class LoggerMixin(Mixin):
    def __init__(self):
        super().__init__()
        self.logs = []
        self.prefix = ""
    
    def mix_init(self, prefix: str = "", **kwargs):
        self.prefix = prefix
    
    @export
    def log(self, message: str):
        self.logs.append(f"{self.prefix}{message}")
        return len(self.logs)

# Create a mixer and add the mixin
mixer = Mixer()
mixer.add_mixin("logger", LoggerMixin, prefix="[INFO] ")

# Use the exported method
mixer.log("Hello world!")  # Returns: 1
print(mixer.logger.logs)   # Prints: ['[INFO] Hello world!']

Core Concepts

Mixins

Mixins are classes that inherit from Mixin and provide functionality that can be mixed into a Mixer instance. Methods can be marked for export using the @export decorator, making them directly accessible from the mixer instance.

class CounterMixin(Mixin):
    def __init__(self):
        super().__init__()
        self.value = 0
    
    @export
    def increment(self):
        self.value += 1
        return self.value

Initialization

Mixins can define an optional mix_init method that will be called when the mixin is added to a mixer. Any additional keyword arguments passed to add_mixin will be forwarded to mix_init.

class MathMixin(Mixin):
    def __init__(self):
        super().__init__()
        self.precision = 2
    
    def mix_init(self, precision: int = 2, **kwargs):
        self.precision = precision
    
    @export
    def add(self, a: float, b: float) -> float:
        return round(a + b, self.precision)

mixer.add_mixin("math", MathMixin, precision=3)

Method Export

Methods marked with @export are made available directly on the mixer instance. If multiple mixins try to export methods with the same name, only the first one is exported and conflicts are tracked.

# Method available directly on mixer
result = mixer.add(1.23, 4.56)

# Access through mixin instance
result = mixer.math.add(1.23, 4.56)

# Check for conflicts
conflicts = mixer.get_conflicts()

Mixin Coordination

Mixins can coordinate with each other through the mixer instance. By default, the mixer is accessible via the mixer property, but you can customize this name to avoid conflicts or improve readability:

# Using default 'mixer' attribute
class WorkerMixin(Mixin):
    @export
    def do_work(self):
        self.mixer.log("Starting work")  # Use another mixin's exported method
        result = self.perform_work()
        self.mixer.logger.log("Done!")   # Access mixin instance directly
        return result

# Using custom mixer attribute name
class DatabaseMixin(Mixin, mixer_attr='app'):
    @export
    def query(self, sql: str):
        # Access mixer through custom name
        logger = self.app.logger
        logger.log(f"Executing: {sql}")
        return self.execute_query(sql)

API Reference

Mixer

  • add_mixin(name: str, mixin_class: Type[Mixin], **kwargs) -> Mixin
  • remove_mixin(name: str) -> None
  • get_mixin(name: str) -> Mixin
  • get_mixins() -> Dict[str, Mixin]
  • get_conflicts() -> Dict[str, List[str]]

Mixin

  • mix_init(**kwargs) -> None - Optional initialization method
  • cleanup() -> None - Clean up resources when removed
  • mixer property - Default access to mixer instance
  • mixer_attr class parameter - Customize mixer attribute name (e.g. class MyMixin(Mixin, mixer_attr='app'))

Decorators

  • @export - Mark a method for export to the mixer namespace

License

MIT License

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

mixit-0.6.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mixit-0.6.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file mixit-0.6.0.tar.gz.

File metadata

  • Download URL: mixit-0.6.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mixit-0.6.0.tar.gz
Algorithm Hash digest
SHA256 3a7e38ba4b2044eb4c4e6bd73dde26e1cdf9497cdb6eae2ab8bcaa9fbc489078
MD5 f2766325f315c85eb849e7b9cc20fcad
BLAKE2b-256 ea616b7c963c081b15220fc498229bbae610329dc2193f8cc9cd371645c3b7a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for mixit-0.6.0.tar.gz:

Publisher: publish.yml on joshms123/mixit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mixit-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: mixit-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mixit-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 86f6c9d4d19aabff7fed75e8ecec9695d05bb1badf57f8bf77161476733a85d2
MD5 36b5c845e5f8b0b7c15df3de594849c9
BLAKE2b-256 9d5368897ade8251b8ed4bc62ff50a1d6a31045debc61592910548a879e40b92

See more details on using hashes here.

Provenance

The following attestation bundles were made for mixit-0.6.0-py3-none-any.whl:

Publisher: publish.yml on joshms123/mixit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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