Skip to main content

import micro-registry

micro-registry is a Python library that provides a flexible and dynamic way to manage classes and their instances. It simplifies the development of complex systems by introducing mechanisms for dynamic class registration, instance creation, and configuration loading from YAML files. The library also implements the MicroComponent pattern and provides built-in RESTful APIs for component management.

Features

  • Dynamic Class Registration: Easily register classes using decorators for dynamic instantiation.
  • MicroComponent Pattern: Build hierarchical component systems with parent-child relationships.
  • Instance Management: Create and manage instances of registered classes with ease.
  • YAML Configuration Loading: Define your system components in YAML files for flexible configuration.
  • Use regular Modules: Register instances of class from regular python packages.
  • Built-in RESTful APIs: Use the RegistryAPI and ComponentAPI to interact with your components over HTTP.
  • Plugin Architecture Support: Load modules dynamically to extend functionality without modifying the core system.

Installation

You can install the package via pip:

pip install micro-registry

Or clone the repository and install it locally:

git clone https://github.com/yourusername/micro-registry.git
cd micro-registry
pip install .

Getting Started

Registering Classes

Registering classes is straightforward using the @register_class decorator. This allows the class to be dynamically instantiated later.

from micro_registry.registry import register_class

@register_class
class MyComponent:
    def __init__(self, name, **kwargs):
        self.name = name
        # Additional initialization

    def start(self):
        print(f"Component {self.name} started.")

    def stop(self):
        print(f"Component {self.name} stopped.")

The MicroComponent Pattern

The MicroComponent pattern provides a base class for creating components with hierarchical relationships. Components can have parents and children, making it easy to build complex systems.

from micro_registry.component import MicroComponent

@register_class
class MyMicroComponent(MicroComponent):
    def __init__(self, name, parent=None, **kwargs):
        super().__init__(name, parent)
        # Additional initialization

Loading Components from YAML

Define your system's components in a YAML file for easy configuration and modification.

components.yaml

components:
  - name: component_a
    class: MyMicroComponent
    parameters:
      param1: value1

  - name: component_b
    class: MyMicroComponent
    parameters:
      param2: value2
    children:
      - name: component_c
        class: MyMicroComponent
        parameters:
          param3: value3

Load the components and start the system:

from micro_registry.component_loader import load_components_and_start_system

load_components_and_start_system('components.yaml')

Using the RegistryAPI and ComponentAPI

micro-registry includes built-in RESTful APIs for interacting with your components.

RegistryAPI

The RegistryAPI provides endpoints for:

  • Listing registered classes.
  • Listing instances.
  • Loading instances from YAML.
  • Filtering instances by base class.

ComponentAPI

The ComponentAPI allows you to:

  • Retrieve component hierarchies.
  • Get and update component attributes.
  • Invoke component methods.

Example:

from micro_registry.registry_rest_api import RegistryRestApi
from micro_registry.component_rest_api import ComponentRestApi

# Initialize the Registry API
registry_api = RegistryRestApi(name='registry_api', host='0.0.0.0', port=8000)

# Add the Component API as a child
component_api = ComponentRestApi(name='component_api', parent=registry_api)

# Start the API server
registry_api.start()

Full Example

components.yaml

components:
  - name: registry_api
    class: RegistryRestApi
    parameters:
      host: "0.0.0.0"
      port: 8000
    children:
      - name: component_api
        class: ComponentRestApi

  - name: scheduler_main
    class: Scheduler
    children:
      - name: living_room_light
        class: Light
        parameters:
          location: "Living Room"
          brightness: 75

      - name: hallway_thermostat
        class: Thermostat
        parameters:
          location: "Hallway"
          temperature: 21.5

      - name: evening_lights_automation
        class: Automation
        parameters:
          action: "turn_on"
          target_devices:
            - "living_room_light"

      - name: morning_temperature_automation
        class: Automation
        parameters:
          action: "set_temperature"
          target_devices:
            - "hallway_thermostat"
          temperature: 23.0

main.py

from micro_registry.component_loader import load_components_and_start_system

if __name__ == '__main__':
    load_components_and_start_system('components.yaml')

Interacting with the API

Once your system is running, you can interact with it using the API.

  • List Instances

    curl http://localhost:8000/api/v1/instances
    
  • Get Component Hierarchy

    curl http://localhost:8000/api/v1/component/scheduler_main/hierarchy/
    
  • Update Component Property

    curl -X POST http://localhost:8000/api/v1/component/living_room_light/update-property/ \
         -H 'Content-Type: application/json' \
         -d '{"property_name": "status", "value": "on"}'
    

Advanced Usage

Plugin Architecture

Load additional modules dynamically to extend the functionality of your system without modifying the core code.

from micro_registry.registry import load_modules_from_directory

load_modules_from_directory('plugins')

Custom Component Methods

Define custom methods in your components and expose them via the API.

@register_class
class Device(MicroComponent):
    def __init__(self, name, device_type, location, **kwargs):
        super().__init__(name)
        self.device_type = device_type
        self.location = location

    def turn_on(self):
        print(f"{self.device_type.capitalize()} '{self.name}' at '{self.location}' is now ON.")

    def turn_off(self):
        print(f"{self.device_type.capitalize()} '{self.name}' at '{self.location}' is now OFF.")

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository on GitHub.

  2. Clone your fork locally.

    git clone https://github.com/yourusername/micro_registry.git
    
  3. Install development dependencies.

    pip install -e .[dev]
    
  4. Create a new branch for your feature or bugfix.

    git checkout -b feature/new-feature
    
  5. Make your changes, write tests, and ensure all tests pass.

    flake8 .
    python -m unittest discover -s tests
    
  6. Commit your changes with a descriptive commit message.

    git commit -am "Add new feature to improve X"
    
  7. Push to your fork and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For questions or contributions, reach out at aleksander.stanik@hammerheadsengineers.com.

Release files for micro-registry 0.4.3

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

Source distribution (sdist)

Source distribution for micro-registry 0.4.3
File Size Uploaded
micro_registry-0.4.3.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for micro-registry 0.4.3
File Interpreter ABI Platform
micro_registry-0.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 36.8 kB

Release files / micro_registry-0.4.3.tar.gz

Download URL micro_registry-0.4.3.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2698910a5ce6f05217c4e2a13879c70b7e15bba0ecea84b3a39efc91dbfb3360
BLAKE2b-256 checksum
How to use checksums
c48a1324dd41d35bd38a7d862b1043d19b620648ea0809360d1047e026ead00d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / micro_registry-0.4.3-py3-none-any.whl

Download URL micro_registry-0.4.3-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97d951c7893dc9c3c9a27d5d4ecb35fdd0a9e7fb87d6a9995e2dac22ae0940d0
BLAKE2b-256 checksum
How to use checksums
2259993d836959dc15dc6c06bf1cc82cd536d1ac8a2bedef8d3672d716511a71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.4.3 This release

2 release files

0.4.2

2 release files

0.4.1

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