|
| 1 | +""" |
| 2 | +Representing a LiDAR Oscillation |
| 3 | +""" |
| 4 | + |
| 5 | +class LidarReading: |
| 6 | + """ |
| 7 | + Class to represent a LiDAR oscillation with distance and angle readings. |
| 8 | + """ |
| 9 | + |
| 10 | + __create_key = object() |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def create(cls, distance: float, angle: float) -> "tuple[bool, LidarReading | None]": |
| 14 | + """ |
| 15 | + Create a new LidarReading object. |
| 16 | + """ |
| 17 | + # Ensure valid input data |
| 18 | + if distance < 0 or angle < -170 or angle > 170: |
| 19 | + return False, None |
| 20 | + |
| 21 | + return True, LidarReading(cls.__create_key, distance, angle) |
| 22 | + |
| 23 | + def __init__(self, class_private_create_key: object, distance: float, angle: float) -> None: |
| 24 | + """ |
| 25 | + Private constructor, use create() method for instantiation |
| 26 | + """ |
| 27 | + assert class_private_create_key is LidarReading.__create_key, "Use the create() method" |
| 28 | + |
| 29 | + # Store the distance and angle for this oscillation |
| 30 | + self.distance = distance |
| 31 | + self.angle = angle |
| 32 | + |
| 33 | + def __str__(self) -> str: |
| 34 | + """ |
| 35 | + Return a string representation of the LiDAR oscillation data. |
| 36 | + """ |
| 37 | + return f"LidarReading: {self.distance} m at {self.angle} degrees" |
| 38 | + |
| 39 | +class LidarOscillation: |
| 40 | + """ |
| 41 | + Class to represent a collection of LiDAR readings that make up an oscillation. |
| 42 | + """ |
| 43 | + |
| 44 | + __create_key = object() |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def create(cls, readings: list[LidarReading]) -> "tuple[bool, LidarOscillation | None]": |
| 48 | + """ |
| 49 | + Create a new LidarOscillation object from a list of LidarReading objects. |
| 50 | + """ |
| 51 | + # Ensuring the list only contains LidarReading instances |
| 52 | + if not all(isinstance(reading, LidarReading) for reading in readings): |
| 53 | + return False, None |
| 54 | + |
| 55 | + return True, LidarOscillation(cls.__create_key, readings) |
| 56 | + |
| 57 | + def __init__(self, class_private_create_key: object, readings: list) -> None: |
| 58 | + """ |
| 59 | + Private constructor, use create() method to instantiate. |
| 60 | + """ |
| 61 | + assert class_private_create_key is LidarOscillation.__create_key, "Use the create() method" |
| 62 | + |
| 63 | + # Store the list of LidarReading objects |
| 64 | + self.readings = readings |
| 65 | + |
| 66 | + def __str__(self) -> str: |
| 67 | + """ |
| 68 | + Return a string representation of the LiDAR oscillation data. |
| 69 | + """ |
| 70 | + reading_strs = [str(reading) for reading in self.readings] # Create a list of reading strings |
| 71 | + return f"LidarOscillation with {len(self.readings)} readings:\n" + "\n".join(reading_strs) |
| 72 | + |
| 73 | + |
0 commit comments