Skip to content

Commit

Permalink
Merge pull request #136 from LaboratoireMecaniqueLille/feature/includ…
Browse files Browse the repository at this point in the history
…e_new_features_from_3.8_and_3.9

Include new features from Python 3.8 and 3.9
  • Loading branch information
WeisLeDocto authored Oct 30, 2024
2 parents 7544b2b + e303137 commit e180830
Show file tree
Hide file tree
Showing 139 changed files with 634 additions and 553 deletions.
12 changes: 6 additions & 6 deletions examples/blocks/synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"""

import crappy
from typing import Tuple, Dict, Optional
from typing import Optional
from time import sleep
import random

Expand All @@ -43,8 +43,8 @@ class RandomPath(crappy.blocks.generator_path.meta_path.Path):
"""

def __init__(self,
time_range: Tuple[float, float],
value_range: Tuple[float, float]) -> None:
time_range: tuple[float, float],
value_range: tuple[float, float]) -> None:
"""Sets the arguments and initializes the parent class.
Args:
Expand All @@ -55,10 +55,10 @@ def __init__(self,

super().__init__()

self._time_range: Tuple[float, float] = time_range
self._value_range: Tuple[float, float] = value_range
self._time_range: tuple[float, float] = time_range
self._value_range: tuple[float, float] = value_range

def get_cmd(self, data: Dict[str, list]) -> Optional[float]:
def get_cmd(self, data: dict[str, list]) -> Optional[float]:
"""Returns a randomly generated value after sleeping a random number of
seconds."""

Expand Down
3 changes: 2 additions & 1 deletion examples/custom_objects/custom_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"""

import crappy
from typing import Optional, Iterable
from typing import Optional
from collections.abc import Iterable
import logging
from time import sleep, time

Expand Down
3 changes: 1 addition & 2 deletions examples/custom_objects/custom_camera/custom_camera_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import crappy
import numpy as np
import numpy.random as rd
from typing import Tuple
from time import time


Expand Down Expand Up @@ -76,7 +75,7 @@ def open(self, **kwargs) -> None:
# Here the Camera does not include settings though
self.set_all(**kwargs)

def get_image(self) -> Tuple[float, np.ndarray]:
def get_image(self) -> tuple[float, np.ndarray]:
"""This method must return the current timestamp as well as an acquired
image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import crappy
import numpy as np
import numpy.random as rd
from typing import Tuple
from time import time, sleep


Expand Down Expand Up @@ -100,7 +99,7 @@ def open(self, **kwargs) -> None:
# This line is mandatory here for first applying the trigger setting
self.set_all(**kwargs)

def get_image(self) -> Tuple[float, np.ndarray]:
def get_image(self) -> tuple[float, np.ndarray]:
"""Compared to the one in custom_camera_basic.py, this method returns the
same images but with a delay if in Hardware trigger mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import crappy
import numpy as np
import numpy.random as rd
from typing import Tuple, Dict, Any
from typing import Any
from time import time, strftime, gmtime


Expand Down Expand Up @@ -69,7 +69,7 @@ def open(self, **kwargs) -> None:
# Here the Camera does not include settings though
self.set_all(**kwargs)

def get_image(self) -> Tuple[Dict[str, Any], np.ndarray]:
def get_image(self) -> tuple[dict[str, Any], np.ndarray]:
"""Compared to the custom_camera_basic.py example, this method returns a
complete dictionary of metadata instead of just a timestamp.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import crappy
import numpy as np
import numpy.random as rd
from typing import Tuple
from time import time


Expand Down Expand Up @@ -109,7 +108,7 @@ def open(self, **kwargs) -> None:
# This line is mandatory here for first applying the instantiated settings
self.set_all(**kwargs)

def get_image(self) -> Tuple[float, np.ndarray]:
def get_image(self) -> tuple[float, np.ndarray]:
"""This method is an extension of the custom_camera_basic.py one.
Instead of just generating the random image always in a same way, it is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import crappy
import numpy as np
from typing import Tuple
from time import time


Expand Down Expand Up @@ -83,7 +82,7 @@ def open(self, **kwargs) -> None:
# This line is mandatory here for first applying the parameters of the ROI
self.set_all(**kwargs)

def get_image(self) -> Tuple[float, np.ndarray]:
def get_image(self) -> tuple[float, np.ndarray]:
"""Compared to the one in custom_camera_basic.py, this method returns the
static image cropped by the selected software ROI.
Expand Down
7 changes: 4 additions & 3 deletions examples/custom_objects/custom_camera_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import crappy
import cv2
import numpy as np
from typing import Optional, Callable, Tuple, Union
from typing import Optional, Union
from collections.abc import Callable
from pathlib import Path


Expand Down Expand Up @@ -92,7 +93,7 @@ def draw(self, img: np.ndarray) -> None:
cv2.ellipse(img,
(self._center_x, self._center_y),
(self._x_axis, self._y_axis),
0, 0, 360, 0, thickness)
0., 0., 360., (0.,), thickness)


class CustomCameraProcess(crappy.blocks.camera_processes.CameraProcess):
Expand Down Expand Up @@ -226,7 +227,7 @@ def __init__(self,
save_backend: Optional[str] = None,
image_generator: Optional[Callable[[float, float],
np.ndarray]] = None,
img_shape: Optional[Tuple[int, int]] = None,
img_shape: Optional[tuple[int, int]] = None,
img_dtype: Optional[str] = None,
scale_factor: float = 1.2,
min_neighbors: int = 3,
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_objects/custom_generator_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import crappy
from crappy.blocks.generator_path.meta_path import Path, ConditionType
from typing import Optional, Union, Dict
from typing import Optional, Union
from math import pi, sin, copysign
from time import time

Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(self,
self._amplitude = amplitude / 2
self._offset = offset

def get_cmd(self, data: Dict[str, list]) -> float:
def get_cmd(self, data: dict[str, list]) -> float:
"""This method is the one that generates and returns the value to output
for the Generator Block.
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_objects/custom_inout/custom_inout_basic_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"""

import crappy
from typing import Tuple, Optional
from typing import Optional
from time import time


Expand Down Expand Up @@ -66,7 +66,7 @@ def open(self) -> None:

...

def get_data(self) -> Tuple[float, int]:
def get_data(self) -> tuple[float, int]:
"""This method is used for acquiring data from the hardware.
Here, it simply updates the loop counter and returns its value along with a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"""

import crappy
from typing import Dict, Optional
from typing import Optional
from time import time


Expand Down Expand Up @@ -64,7 +64,7 @@ def open(self) -> None:

...

def get_data(self) -> Dict[str, float]:
def get_data(self) -> dict[str, float]:
"""This method is used for acquiring data from the hardware.
Here, it returns the current timestamp as well as the value of the _buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import crappy
from random import random
from typing import Tuple, Optional
from typing import Optional
from time import time


Expand Down Expand Up @@ -67,7 +67,7 @@ def open(self) -> None:

...

def get_data(self) -> Tuple[float, float]:
def get_data(self) -> tuple[float, float]:
"""This method is used for acquiring data from the hardware.
Here, it returns the current timestamp and a random value in the interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import crappy
import numpy as np
import numpy.random as rd
from typing import Tuple
from time import time


Expand Down Expand Up @@ -70,7 +69,7 @@ def start_stream(self) -> None:

...

def get_stream(self) -> Tuple[np.ndarray, np.ndarray]:
def get_stream(self) -> tuple[np.ndarray, np.ndarray]:
"""This method acquires the stream data and returns it in one array
containing the time information and another array containing the data.
Expand Down
7 changes: 5 additions & 2 deletions examples/custom_objects/custom_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
"""

import crappy
from typing import Dict, Any, Iterable, Optional
from typing import Optional, TypeVar
from collections.abc import Iterable
from math import sqrt

T = TypeVar('T')


class CustomModifier(crappy.modifier.Modifier):
"""This class demonstrates the instantiation of a custom Modifier object in
Expand Down Expand Up @@ -73,7 +76,7 @@ def __init__(self,
self._square_sum = 0
self._nb_samples = 0

def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
def __call__(self, data: dict[str, T]) -> dict[str, T]:
"""This method is the one that transforms the received data and returns the
modified version.
Expand Down
4 changes: 1 addition & 3 deletions src/crappy/actuator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# coding: utf-8

from typing import Dict, Type

from .adafruit_dc_motor_hat import DCMotorHat
from .fake_dc_motor import FakeDCMotor
from .fake_stepper_motor import FakeStepperMotor
Expand All @@ -18,4 +16,4 @@
from .meta_actuator import MetaActuator, Actuator

from ._deprecated import deprecated_actuators
actuator_dict: Dict[str, Type[Actuator]] = MetaActuator.classes
actuator_dict: dict[str, type[Actuator]] = MetaActuator.classes
6 changes: 3 additions & 3 deletions src/crappy/actuator/adafruit_dc_motor_hat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from struct import pack_into
from time import sleep
from typing import Union, Tuple
from typing import Union, Literal
import logging
from warnings import warn

Expand Down Expand Up @@ -68,7 +68,7 @@ class DCMotorHat(Actuator):
"""

def __init__(self,
backend: str,
backend: Literal['Pi4', 'blinka'],
device_address: int = 0x60,
i2c_port: int = 1) -> None:
"""Checks the validity of the arguments.
Expand Down Expand Up @@ -142,7 +142,7 @@ def open(self) -> None:
for i in range(1, 5):
self._write_i2c(motor_hat_ctrl[i], motor_hat_0xFF)

def set_speed(self, cmd: Tuple[int, float]) -> None:
def set_speed(self, cmd: tuple[int, float]) -> None:
"""Sets the desired voltage on the selected motor.
The provided voltage should be between `-12` and `12V` which are the limits
Expand Down
4 changes: 2 additions & 2 deletions src/crappy/actuator/ft232h/adafruit_dc_motor_hat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from struct import pack_into
from time import sleep
from typing import Union, Optional, Tuple
from typing import Union, Optional
import logging
from warnings import warn

Expand Down Expand Up @@ -114,7 +114,7 @@ def open(self) -> None:
for i in range(1, 5):
self._write_i2c(motor_hat_ctrl[i], motor_hat_0xFF)

def set_speed(self, cmd: Tuple[int, float]) -> None:
def set_speed(self, cmd: tuple[int, float]) -> None:
"""Sets the desired voltage on the selected motor.
The provided voltage should be between `-12` and `12V` which are the limits
Expand Down
6 changes: 3 additions & 3 deletions src/crappy/actuator/jvl_mac_140.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8

from struct import pack, unpack
from typing import Optional, Tuple
from typing import Optional
from time import sleep
import logging

Expand Down Expand Up @@ -201,8 +201,8 @@ def reset_position(self) -> None:
pass

def _make_cmd(self,
values: Tuple[int, int, int, int],
encodings: Tuple[str, str, str, str],
values: tuple[int, int, int, int],
encodings: tuple[str, str, str, str],
last_cmd: bool) -> bytes:
"""Builds a command to send to the servomotor, from the given arguments.
Expand Down
4 changes: 2 additions & 2 deletions src/crappy/actuator/kollmorgen_servostar_300.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8

from typing import Union, Optional
from typing import Union, Optional, Literal
import logging

from .meta_actuator import Actuator
Expand All @@ -27,7 +27,7 @@ class ServoStar300(Actuator):
def __init__(self,
port: str,
baudrate: int = 38400,
mode: str = "serial") -> None:
mode: Literal['serial', 'analog'] = "serial") -> None:
"""Sets the instance attributes and initializes the parent class.
Args:
Expand Down
3 changes: 1 addition & 2 deletions src/crappy/actuator/oriental_ard_k.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def set_speed(self, cmd: float) -> None:
return

# Stopping the motor if the direction changed
dir_chg = self._prev_set_speed * sign < 0
if dir_chg:
if self._prev_set_speed * sign < 0:
self.stop()

# Writing the target speed value
Expand Down
4 changes: 2 additions & 2 deletions src/crappy/actuator/phidgets_stepper4a.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from pathlib import Path
from platform import system
from typing import Optional, Tuple, Union
from typing import Optional, Union

from .meta_actuator import Actuator
from .._global import OptionalModule
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(self,
remote: bool = False,
absolute_mode: bool = False,
reference_pos: float = 0,
switch_ports: Tuple[int, ...] = tuple(),
switch_ports: tuple[int, ...] = tuple(),
save_last_pos: bool = False,
save_pos_folder: Optional[Union[str, Path]] = None) -> None:
"""Sets the args and initializes the parent class.
Expand Down
Loading

0 comments on commit e180830

Please sign in to comment.