Skip to content

Commit a710405

Browse files
authored
Modifying params to data encoding functions (#77)
* Changed data encoding functions to take in WorkerEnum, fixed typo. * fixed unit tests * fixed linting * made requested changes * fixed linting
1 parent 9acf88b commit a710405

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

modules/data_encoding/message_encoding_decoding.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@
1010

1111

1212
def encode_position_global(
13-
worker_name: str, global_position: position_global.PositionGlobal
13+
worker_id: worker_enum.WorkerEnum, global_position: position_global.PositionGlobal
1414
) -> "tuple[True, bytes] | tuple[False, None]":
1515
"""
1616
Encode PositionGlobal object into Bytes. Worker_ID to be encoded as the first byte of the message
1717
1818
Parameters:
19-
worker_name: name of the worker defined by the name of its own file (worker_name = pathlib.Path(__file__).stem)
19+
worker_id: ID of the worker defined by its constant in WorkerEnum
2020
global_position: PositionGlobal object
2121
2222
Returns:
2323
packed_coordinates (bytes): Encoded latitude, longitude, altitude of PositionGlobal object as bytes.
2424
First byte dependant on which worker is calling the funciton, value depends on its corresponding enum value.
2525
"""
2626
try:
27-
worker_id = worker_enum.WorkerEnum[worker_name.upper()]
28-
if not worker_id: # If worker ID is not in the Enum Class
27+
if not isinstance(
28+
worker_id, worker_enum.WorkerEnum
29+
): # If worker ID is not in the Enum Class
2930
return False, None
3031

3132
# Encode message using PositionGlobal's latitude, longitude, altitude, with the worker ID in the front

modules/data_encoding/metadata_encoding_decoding.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99

1010

1111
def encode_metadata(
12-
worker_name: str, number_of_messages: int
12+
worker_id: worker_enum.WorkerEnum, number_of_messages: int
1313
) -> "tuple[True, bytes] | tuple[False, None]":
1414
"""
1515
Encode PositionGlobal object into Bytes. Worker_ID to be encoded as the first byte of the message
1616
1717
Parameters:
18-
worker_name: name of the worker defined by the name of its own file (worker_name = pathlib.Path(__file__).stem)
18+
worker_id: ID of the worker defined by its constant in WorkerEnum
1919
number_of_messages: number of messages intended to be sent
2020
2121
Returns:
2222
packed_coordinates (bytes): Encoded int corresponding to number of messages as bytes.
2323
First byte dependant on which worker is calling the funciton, value depends on its corresponding enum value (see worker_enum.py)
2424
"""
2525
try:
26-
worker_id = worker_enum.WorkerEnum[worker_name.upper()]
27-
if not worker_id: # If worker ID is not in the Enum Class
26+
if not isinstance(
27+
worker_id, worker_enum.WorkerEnum
28+
): # If worker ID is not in the Enum Class
2829
return False, None
2930

3031
# Encode message using PositionGlobal's latitude, longitude, altitude, with the worker ID in the front

modules/data_encoding/worker_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ class WorkerEnum(Enum):
2323
COMMUNICATIONS_WORKER = 2
2424
DATA_MERGE_WORKER = 3
2525
DETECT_TARGET_WORKER = 4
26-
FLIGHT_INTERFERENCE_WORKER = 5
26+
FLIGHT_INTERFACE_WORKER = 5
2727
GEOLOCATION_WORKER = 6
2828
VIDEO_INPUT_WORKER = 7

tests/unit/data_encoding/test_message_encoding_decoding.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ def test_encoding_decoding() -> None:
1111
"""
1212
Function to test encoding
1313
"""
14-
# Step 1: Create a worker_name and PositionGlobal object
15-
worker_name = "communications_worker" # =3 in Worker_Enum
14+
# Step 1: Create a worker_id and PositionGlobal object
15+
worker_id = worker_enum.WorkerEnum.COMMUNICATIONS_WORKER # =3 in Worker_Enum
1616
success, original_position = position_global.PositionGlobal.create(
1717
latitude=34.24902422, longitude=84.6233434, altitude=27.4343424
1818
)
1919
assert success
2020

2121
# Step 2: Encode the PositionGlobal object
2222
result, encoded_bytes = message_encoding_decoding.encode_position_global(
23-
worker_name, original_position
23+
worker_id, original_position
2424
)
2525
assert result
2626

@@ -31,7 +31,7 @@ def test_encoding_decoding() -> None:
3131
assert result
3232

3333
# Step 4: Validate that the original and decoded objects match
34-
assert worker_enum.WorkerEnum[worker_name.upper()] == worker # Checks if Enum type Matches
34+
assert worker_id == worker # Checks if Enum type Matches
3535

3636
assert original_position.latitude == decoded_position.latitude
3737
assert original_position.longitude == decoded_position.longitude

tests/unit/data_encoding/test_metadata_encoding_decoding.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def test_encoding_metadata() -> None:
1010
"""
1111
Function to test encoding
1212
"""
13-
# Step 1: Create a worker_name and PositionGlobal object
14-
worker_name = "communications_worker" # =3 in worker_enum.py
13+
# Step 1: Create a worker_id and PositionGlobal object
14+
worker_id = worker_enum.WorkerEnum.COMMUNICATIONS_WORKER # =3 in worker_enum.py
1515
number_of_messages = 5
1616

1717
# Step 2: Encode the WorkerEnum ID and number of messages
1818
result, encoded_bytes = metadata_encoding_decoding.encode_metadata(
19-
worker_name, number_of_messages
19+
worker_id, number_of_messages
2020
)
2121
assert result
2222

@@ -27,8 +27,6 @@ def test_encoding_metadata() -> None:
2727
assert result
2828

2929
# Step 4: Validate that the original and decoded objects match
30-
assert (
31-
worker_enum.WorkerEnum[worker_name.upper()] == worker_class
32-
) # Checks if Enum type Matches
30+
assert worker_id == worker_class # Checks if Enum type Matches
3331

3432
assert number_of_messages == decoded_number_of_messages

0 commit comments

Comments
 (0)