Skip to content

factory

Factory Utility API

This module provides a thread-safe singleton factory to register, create, and list items.

Classes:

Classes

SingletonMeta

Thread-safe singleton.

ItemFactory

ItemFactory()

Item factory enables registering, creating, and listing items. It is a singleton class.

Source code in neuralspot_edge/utils/factory.py
def __init__(self):
    self._items = {}

Functions

shared classmethod
shared(factory: str) -> ItemFactory

Get the shared instance of the factory

Returns:

Source code in neuralspot_edge/utils/factory.py
@classmethod
def shared(cls, factory: str) -> "ItemFactory":
    """Get the shared instance of the factory

    Returns:
        ItemFactory: shared instance
    """
    return cls(singleton=factory)
register
register(name: str, item: T) -> None

Register an item

Parameters:

  • name (str) –

    Unique item name

  • item (T) –

    Item

Source code in neuralspot_edge/utils/factory.py
def register(self, name: str, item: T) -> None:
    """Register an item

    Args:
        name (str): Unique item name
        item (T): Item
    """
    self._items[name] = item
unregister
unregister(name: str) -> None

Unregister an item

Parameters:

  • name (str) –

    Item name

Source code in neuralspot_edge/utils/factory.py
def unregister(self, name: str) -> None:
    """Unregister an item

    Args:
        name (str): Item name
    """
    self._items.pop(name, None)
list
list() -> list[str]

List registered items

Returns:

  • list[str]

    list[str]: item names

Source code in neuralspot_edge/utils/factory.py
def list(self) -> list[str]:
    """List registered items

    Returns:
        list[str]: item names
    """
    return list(self._items.keys())
get
get(name: str) -> T

Get an item

Parameters:

  • name (str) –

    Item name

Returns:

  • T ( T ) –

    Item

Source code in neuralspot_edge/utils/factory.py
def get(self, name: str) -> T:
    """Get an item

    Args:
        name (str): Item name

    Returns:
        T: Item
    """
    return self._items[name]
has
has(name: str) -> bool

Check if an item is registered

Parameters:

  • name (str) –

    Item name

Returns:

  • bool ( bool ) –

    True if item is registered

Source code in neuralspot_edge/utils/factory.py
def has(self, name: str) -> bool:
    """Check if an item is registered

    Args:
        name (str): Item name

    Returns:
        bool: True if item is registered
    """
    return name in self._items

Functions

create_factory

create_factory(factory: str, type: Type[T]) -> ItemFactory[T]

Create a factory

Parameters:

  • factory (str) –

    Factory name

  • type (Type[T]) –

    Item type

Returns:

Source code in neuralspot_edge/utils/factory.py
def create_factory(factory: str, type: Type[T]) -> ItemFactory[T]:
    """Create a factory

    Args:
        factory (str): Factory name
        type (Type[T]): Item type

    Returns:
        ItemFactory[T]: factory
    """
    return ItemFactory[T].shared(factory)