Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hold state in a class instead of globals #790

Open
stan-dot opened this issue Sep 16, 2024 · 1 comment
Open

Hold state in a class instead of globals #790

stan-dot opened this issue Sep 16, 2024 · 1 comment
Labels
enhancement New feature or request python Pull requests that update Python code

Comments

@stan-dot
Copy link
Contributor

It's a maintainability issue, as the state of the whole application, such as blueapi using this library can be subject to hard to predict changes.

OOP takes care to only allow state mutation through setters, at the moment this is quite haphazard.

This change would definitely be a breaking one to the downstream projects, however not too difficult to implement here.

@stan-dot stan-dot added enhancement New feature or request python Pull requests that update Python code labels Sep 16, 2024
@stan-dot
Copy link
Contributor Author

example implementation:

from typing import Final, Type, Optional

class DeviceManager:
    DEFAULT_CONNECTION_TIMEOUT: Final[float] = 5.0

    def __init__(self):
        self.active_devices: dict[str, AnyDevice] = {}
        self.beamline_name: str = ""
        self.path_provider: Optional[UpdatingPathProvider] = None

    def set_beamline(self, beamline: str):
        """Set the beamline name."""
        self.beamline_name = beamline

    def clear_devices(self):
        """Clear all active devices."""
        self.active_devices.clear()

    def clear_device(self, name: str):
        """Clear a specific device by name."""
        self.active_devices.pop(name, None)

    def list_active_devices(self) -> list[str]:
        """List the names of all active devices."""
        return list(self.active_devices.keys())

    def active_device_is_same_type(
        self, active_device: AnyDevice, device: Type[AnyDevice]
    ) -> bool:
        """Check if the active device is of the same type as the provided device."""
        return isinstance(active_device, device)

# Usage Example
manager = DeviceManager()
manager.set_beamline("BL1")
manager.clear_devices()

# Add a device (Example)
manager.active_devices['device1'] = AnyDevice()

# Check if a device is the same type
is_same_type = manager.active_device_is_same_type(manager.active_devices['device1'], AnyDevice)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python Pull requests that update Python code
Projects
None yet
Development

No branches or pull requests

1 participant