Skip to content

Commit

Permalink
Merge pull request #59 from CederGroupHub/Update-to-python=3.10-minimum
Browse files Browse the repository at this point in the history
Update black.yaml, ci.yaml, and page.yaml to python 3.10
  • Loading branch information
bernardusrendy authored Mar 17, 2024
2 parents 4322428 + 1c2aa66 commit 5f13234
Show file tree
Hide file tree
Showing 40 changed files with 252 additions and 277 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/black.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.9" # Specify the Python version you need
python-version: "3.10" # Specify the Python version you need
- name: Install Black
run: pip install black==22.3.0
run: pip install black
- name: Run Black
run: black ./alab_management
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
timeout-minutes: 60
strategy:
matrix:
python-version: [ "3.8", "3.9", "3.10" ]
python-version: ["3.10"]
services:
rabbitmq:
image: "rabbitmq:3.9"
Expand All @@ -37,7 +37,7 @@ jobs:
uses: actions/setup-python@v2
with:
# python-version: ${{ matrix.python-version }}
python-version: "3.9"
python-version: "3.10"
# cache: 'pip'
# cache-dependency-path:
- name: Set up environment
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: '3.10'
- name: Set up dependencies
run: pip install --quiet .
- name: Compile sphinx
Expand Down
20 changes: 10 additions & 10 deletions alab_management/builders/experimentbuilder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Set, Union
from typing import Any, Literal

from .samplebuilder import SampleBuilder

Expand All @@ -13,20 +13,20 @@ class ExperimentBuilder:
name (str): The name of the experiment.
"""

def __init__(self, name: str, tags: Optional[List[str]] = None, **metadata):
def __init__(self, name: str, tags: list[str] | None = None, **metadata):
"""
Args:
name (str): The name of the experiment.
tags (List[str]): A list of tags to attach to the experiment.
"""
self.name = name
self._samples: List[SampleBuilder] = []
self._tasks: Dict[str, Dict[str, Any]] = {}
self._samples: list[SampleBuilder] = []
self._tasks: dict[str, dict[str, Any]] = {}
self.tags = tags or []
self.metadata = metadata

def add_sample(
self, name: str, tags: Optional[List[str]] = None, **metadata
self, name: str, tags: list[str] | None = None, **metadata
) -> SampleBuilder:
"""
Add a sample to the batch. Each sample already has multiple tasks binded to it. Each
Expand All @@ -53,8 +53,8 @@ def add_task(
self,
task_id: str,
task_name: str,
task_kwargs: Dict[str, Any],
samples: List[SampleBuilder],
task_kwargs: dict[str, Any],
samples: list[SampleBuilder],
) -> None:
"""
This function adds a task to the sample. You should use this function only for special cases which
Expand All @@ -78,7 +78,7 @@ def add_task(
"samples": [sample.name for sample in samples],
}

def to_dict(self) -> Dict[str, Any]:
def to_dict(self) -> dict[str, Any]:
"""
Return a dictionary that can be used to generate an input file for the `experiment`
to run.
Expand All @@ -88,9 +88,9 @@ def to_dict(self) -> Dict[str, Any]:
A dictionary that can be used to generate an input file for the `experiment` to run.
"""
samples: List[Dict[str, Any]] = []
samples: list[dict[str, Any]] = []
# tasks = []
tasks: List[Dict[str, Union[str, Set[int], List]]] = []
tasks: list[dict[str, str | set[int] | list]] = []
task_ids = {}

for sample in self._samples:
Expand Down
8 changes: 4 additions & 4 deletions alab_management/builders/samplebuilder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Build the sample object."""

from typing import TYPE_CHECKING, Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any

from bson import ObjectId # type: ignore

Expand All @@ -23,11 +23,11 @@ def __init__(
self,
name: str,
experiment: "ExperimentBuilder",
tags: Optional[List[str]] = None,
tags: list[str] | None = None,
**metadata,
):
self.name = name
self._tasks: List[str] = [] # type: ignore
self._tasks: list[str] = [] # type: ignore
self.experiment = experiment
self.metadata = metadata
self._id = str(ObjectId())
Expand All @@ -49,7 +49,7 @@ def add_task(
if task_id not in self._tasks:
self._tasks.append(task_id)

def to_dict(self) -> Dict[str, Any]:
def to_dict(self) -> dict[str, Any]:
"""Return Sample as a dictionary.
This looks like:
Expand Down
4 changes: 2 additions & 2 deletions alab_management/builders/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This module contains utility functions for the builders module."""

from typing import TYPE_CHECKING, List, Union
from typing import TYPE_CHECKING

from bson import ObjectId # type: ignore

Expand All @@ -13,7 +13,7 @@

def append_task(
task: "BaseTask",
samples: Union[SampleBuilder, List[SampleBuilder]],
samples: SampleBuilder | list[SampleBuilder],
):
"""
Used to add basetask to a SampleBuilder's tasklist during Experiment construction.
Expand Down
4 changes: 2 additions & 2 deletions alab_management/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import os
from pathlib import Path
from types import MappingProxyType as FrozenDict
from typing import Any, Dict
from typing import Any

import toml


def freeze_config(config_: Dict[str, Any]) -> FrozenDict:
def freeze_config(config_: dict[str, Any]) -> FrozenDict:
"""
Convert the config dict to frozen config.
Expand Down
6 changes: 3 additions & 3 deletions alab_management/dashboard/routes/experiment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This is a dashboard that displays data from the ALab database."""

from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional
from typing import Any

from bson import ObjectId # type: ignore
from bson.errors import InvalidId # type: ignore
Expand Down Expand Up @@ -180,11 +180,11 @@ def query_experiment_results(exp_id: str):
def cancel_experiment(exp_id: str):
try:
exp_id = ObjectId(exp_id)
experiment: Optional[Dict[str, Any]] = experiment_view.get_experiment(exp_id)
experiment: dict[str, Any] | None = experiment_view.get_experiment(exp_id)
if experiment is None:
return {"status": "error", "reason": "Experiment not found"}, 400

tasks: List[Dict[str, Any]] = experiment["tasks"]
tasks: list[dict[str, Any]] = experiment["tasks"]
# tasks = experiment_view.get_experiment(exp_id)["tasks"]

for task in tasks:
Expand Down
13 changes: 6 additions & 7 deletions alab_management/device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
DeviceManager class, which will handle all the request to run certain methods on the real device.
"""

from collections.abc import Callable
from concurrent.futures import Future
from enum import Enum, auto
from functools import partial
from threading import Thread
from typing import Any, Callable, Dict, NoReturn, Optional, cast
from typing import Any, NoReturn, cast
from uuid import uuid4

import dill
Expand Down Expand Up @@ -171,9 +172,7 @@ def callback_publish(channel, delivery_tag, props, response):
channel.basic_ack(delivery_tag=cast(int, delivery_tag))

try:
device_entry: Optional[Dict[str, Any]] = self._device_view.get_device(
device
)
device_entry: dict[str, Any] | None = self._device_view.get_device(device)

# check if the device is currently occupied by this task
if self._check_status and (
Expand Down Expand Up @@ -217,7 +216,7 @@ def on_message(
"kwargs": Dict,
}
"""
body: Dict[str, Any] = dill.loads(_body)
body: dict[str, Any] = dill.loads(_body)

thread = Thread(
target=self._execute_command_wrapper,
Expand Down Expand Up @@ -266,15 +265,15 @@ def __init__(self, task_id: ObjectId, timeout: int = None):
# taskid, or can be random? I think this dies with the resourcerequest context manager anyways?
self._rpc_reply_queue_name = str(uuid4()) + DEFAULT_CLIENT_QUEUE_SUFFIX
self._task_id = task_id
self._waiting: Dict[ObjectId, Future] = {}
self._waiting: dict[ObjectId, Future] = {}

self._conn = get_rabbitmq_connection()
self._channel = self._conn.channel()
self._channel.queue_declare(
self._rpc_reply_queue_name, exclusive=False, auto_delete=True
)

self._thread: Optional[Thread] = None
self._thread: Thread | None = None

self._channel.basic_consume(
queue=self._rpc_reply_queue_name,
Expand Down
6 changes: 3 additions & 3 deletions alab_management/device_view/dbattributes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Union
from typing import Any

from pymongo.collection import Collection # type: ignore

Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(
device_collection: Collection,
device_name: str,
attribute_name: str,
default_value: Union[list, None] = None,
default_value: list | None = None,
):
self._collection = device_collection
self.attribute_name = attribute_name
Expand Down Expand Up @@ -284,7 +284,7 @@ def __init__(
device_collection: Collection,
device_name: str,
attribute_name: str,
default_value: Union[dict, None] = None,
default_value: dict | None = None,
):
self._collection = device_collection
self.attribute_name = attribute_name
Expand Down
27 changes: 14 additions & 13 deletions alab_management/device_view/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import threading
import time
from abc import ABC, abstractmethod
from collections.abc import Callable
from queue import Empty, PriorityQueue
from traceback import format_exc
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any
from unittest.mock import Mock

from alab_management.logger import DBLogger
Expand All @@ -23,7 +24,7 @@ def _UNSPECIFIED(_):

def mock(
return_constant: Any = _UNSPECIFIED,
object_type: Union[List[Any], Any] = _UNSPECIFIED,
object_type: list[Any] | Any = _UNSPECIFIED,
):
"""
A decorator used for mocking functions during simulation.
Expand Down Expand Up @@ -143,7 +144,7 @@ class BaseDevice(ABC):
kwargs: keyword arguments that will be passed to the device class
"""

def __init__(self, name: str, description: Optional[str] = None, *args, **kwargs):
def __init__(self, name: str, description: str | None = None, *args, **kwargs):
"""
Initialize a device object, you can set up connection to
the device in this method. The device will only be initialized
Expand Down Expand Up @@ -253,7 +254,7 @@ def disconnect(self):

@property
@abstractmethod
def sample_positions(self) -> List[SamplePosition]:
def sample_positions(self) -> list[SamplePosition]:
"""
The sample positions describe the position that can hold a sample. The name of sample
position will be the unique identifier of this sample position. It does not store any
Expand Down Expand Up @@ -298,7 +299,7 @@ def is_running(self) -> bool:

# methods to store Device values inside the database. Lists and dictionaries are supported.
def list_in_database(
self, name: str, default_value: Optional[Union[list, None]] = None
self, name: str, default_value: list | None | None = None
) -> ListInDatabase:
"""
Create a list attribute that is stored in the database.
Expand All @@ -320,7 +321,7 @@ def list_in_database(
)

def dict_in_database(
self, name: str, default_value: Optional[Union[dict, None]] = None
self, name: str, default_value: dict | None | None = None
) -> DictInDatabase:
"""
Create a dict attribute that is stored in the database.
Expand Down Expand Up @@ -353,7 +354,7 @@ def _apply_default_db_values(self):
if any(isinstance(attribute, t) for t in [ListInDatabase, DictInDatabase]):
attribute.apply_default_value()

def request_maintenance(self, prompt: str, options: List[Any]):
def request_maintenance(self, prompt: str, options: list[Any]):
"""
Request maintenance input from the user. This will display a prompt to the user and wait for them to select
an option. The selected option will be returned.
Expand All @@ -365,7 +366,7 @@ def request_maintenance(self, prompt: str, options: List[Any]):
return request_maintenance_input(prompt=prompt, options=options)

def retrieve_signal(
self, signal_name: str, within: Optional[datetime.timedelta] = None
self, signal_name: str, within: datetime.timedelta | None = None
):
"""Retrieve a signal from the database.
Expand Down Expand Up @@ -423,8 +424,8 @@ def __init__(self, device: BaseDevice):
self.is_logging = False
self.queue: PriorityQueue = PriorityQueue()

self._logging_thread: Optional[threading.Thread] = None
self._start_time: Optional[datetime.datetime] = None
self._logging_thread: threading.Thread | None = None
self._start_time: datetime.datetime | None = None

def get_methods_to_log(self):
"""
Expand Down Expand Up @@ -567,7 +568,7 @@ def stop(self):
self.is_logging = False
self._logging_thread.join()

def retrieve_signal(self, signal_name, within: Optional[datetime.timedelta] = None):
def retrieve_signal(self, signal_name, within: datetime.timedelta | None = None):
"""Retrieve a signal from the database.
Args:
Expand Down Expand Up @@ -599,7 +600,7 @@ def retrieve_signal(self, signal_name, within: Optional[datetime.timedelta] = No
)


_device_registry: Dict[str, BaseDevice] = {}
_device_registry: dict[str, BaseDevice] = {}


def add_device(device: BaseDevice):
Expand All @@ -609,7 +610,7 @@ def add_device(device: BaseDevice):
_device_registry[device.name] = device


def get_all_devices() -> Dict[str, BaseDevice]:
def get_all_devices() -> dict[str, BaseDevice]:
"""
Get all the device names in the device registry. This is a shallow copy of the registry.
Expand Down
Loading

0 comments on commit 5f13234

Please sign in to comment.