From a8dd23d19a57c3206cb3e593cc9b8801b40e7a6a Mon Sep 17 00:00:00 2001 From: yihong1120 Date: Tue, 22 Oct 2024 22:36:48 +0800 Subject: [PATCH] Adjust layout --- .env | 2 +- src/utils.py | 21 +++++++----- tests/dataset/coco_annotations.json | 2 +- tests/dataset/data.yaml | 4 +-- ...ng.rf.b6c7c864c132216e92be0db773e9fcdd.txt | 2 +- ...ng.rf.f0a76afac7ce00fbf23b8be39ebc3493.txt | 2 +- ...ng.rf.c1d75c2efe355241a72f3c0e873e68e0.txt | 2 +- ...ng.rf.3a96bb284b06d743b2641b3e0f2055a9.txt | 2 +- ...ng.rf.80b34a7c8ba57698ddc5d436539b4576.txt | 2 +- ...ng.rf.14184d8ea6d551ecb099b7cc25b2ce28.txt | 2 +- ...eg.rf.6c9ea300434605e0ac828342b371e204.txt | 2 +- ...eg.rf.b4ba444d690bb533133e22f1bd69442d.txt | 2 +- ...eg.rf.3e98d2f5b90e0b1459e15f570a433459.txt | 2 +- tests/src/utils_test.py | 34 ++++++++++++++----- 14 files changed, 50 insertions(+), 31 deletions(-) diff --git a/.env b/.env index c0b9fa0..d3492ef 100644 --- a/.env +++ b/.env @@ -4,4 +4,4 @@ API_PASSWORD='passcode' API_URL = "http://localhost:5000" redis_host = 'localhost' redis_port = 6379 -redis_password = '' \ No newline at end of file +redis_password = '' diff --git a/src/utils.py b/src/utils.py index 01d363e..a34ac9a 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,11 +1,13 @@ from __future__ import annotations -import os import logging -from redis import Redis +import os from datetime import datetime + +from redis import Redis from watchdog.events import FileSystemEventHandler + class Utils: @staticmethod def is_expired(expire_date_str: str | None) -> bool: @@ -23,7 +25,8 @@ def is_expired(expire_date_str: str | None) -> bool: expire_date = datetime.fromisoformat(expire_date_str) return datetime.now() > expire_date return False - + + class FileEventHandler(FileSystemEventHandler): def __init__(self, file_path: str, callback): """ @@ -31,7 +34,7 @@ def __init__(self, file_path: str, callback): Args: file_path (str): The path of the file to watch. - callback (Callable): The function to call when the file is modified. + callback (Callable): The function to call when file is modified. """ self.file_path = file_path self.callback = callback @@ -46,6 +49,7 @@ def on_modified(self, event): if event.src_path == self.file_path: self.callback() + class RedisManager: def __init__(self): """ @@ -66,7 +70,7 @@ def __init__(self): def set(self, key: str, value: bytes) -> None: """ Set a key-value pair in Redis. - + Args: key (str): The key under which to store the value. value (bytes): The value to store (in bytes). @@ -79,7 +83,7 @@ def set(self, key: str, value: bytes) -> None: def get(self, key: str) -> bytes | None: """ Retrieve a value from Redis based on the key. - + Args: key (str): The key whose value needs to be retrieved. @@ -92,15 +96,14 @@ def get(self, key: str) -> bytes | None: logging.error(f"Error retrieving Redis key {key}: {str(e)}") return None - def delete(self, key: str) -> None: """ Delete a key from Redis. - + Args: key (str): The key to delete from Redis. """ try: self.redis.delete(key) except Exception as e: - logging.error(f"Error deleting Redis key {key}: {str(e)}") \ No newline at end of file + logging.error(f"Error deleting Redis key {key}: {str(e)}") diff --git a/tests/dataset/coco_annotations.json b/tests/dataset/coco_annotations.json index 295b6b1..d8109b8 100644 --- a/tests/dataset/coco_annotations.json +++ b/tests/dataset/coco_annotations.json @@ -551,4 +551,4 @@ "supercategory": "none" } ] -} \ No newline at end of file +} diff --git a/tests/dataset/data.yaml b/tests/dataset/data.yaml index fe82048..0ef15b4 100644 --- a/tests/dataset/data.yaml +++ b/tests/dataset/data.yaml @@ -33,7 +33,7 @@ head: - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 7], 1, Concat, [1]] # cat backbone P4 - [-1, 2, C3k2, [512, False]] # P4/16-large - + # P4 output - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 5], 1, Concat, [1]] # cat backbone P3 @@ -53,4 +53,4 @@ head: - [-1, 1, Conv, [64, 3, 2]] # Downsample for next layer # Detection layers for P2 to P5 outputs - - [[16, 19, 22, 24], 1, Detect, [nc]] # Detect(P2, P3, P4, P5) \ No newline at end of file + - [[16, 19, 22, 24], 1, Detect, [nc]] # Detect(P2, P3, P4, P5) diff --git a/tests/dataset/train/labels/-1-_png.rf.b6c7c864c132216e92be0db773e9fcdd.txt b/tests/dataset/train/labels/-1-_png.rf.b6c7c864c132216e92be0db773e9fcdd.txt index 99fba9d..d4a60f2 100644 --- a/tests/dataset/train/labels/-1-_png.rf.b6c7c864c132216e92be0db773e9fcdd.txt +++ b/tests/dataset/train/labels/-1-_png.rf.b6c7c864c132216e92be0db773e9fcdd.txt @@ -2,4 +2,4 @@ 5 0.5049438202247191 0.8079518072289157 0.1047752808988764 0.38421686746987954 0 0.505056179775281 0.6563855421686746 0.03839887640449438 0.07951807228915662 7 0.5230898876404494 0.8645783132530122 0.05924157303370786 0.27072289156626506 -3 0.5040168539325843 0.7449397590361445 0.02047752808988764 0.07783132530120482 \ No newline at end of file +3 0.5040168539325843 0.7449397590361445 0.02047752808988764 0.07783132530120482 diff --git a/tests/dataset/train/labels/-1-_png.rf.f0a76afac7ce00fbf23b8be39ebc3493.txt b/tests/dataset/train/labels/-1-_png.rf.f0a76afac7ce00fbf23b8be39ebc3493.txt index 4f27236..210ba3a 100644 --- a/tests/dataset/train/labels/-1-_png.rf.f0a76afac7ce00fbf23b8be39ebc3493.txt +++ b/tests/dataset/train/labels/-1-_png.rf.f0a76afac7ce00fbf23b8be39ebc3493.txt @@ -10,4 +10,4 @@ 6 0.2140551181102362 0.823036809815951 0.009015748031496063 0.02361963190184049 6 0.20616141732283466 0.8133435582822085 0.007440944881889763 0.02294478527607362 6 0.09665354330708661 0.8588343558282209 0.011850393700787401 0.03573619631901841 -6 0.29883858267716534 0.7705828220858896 0.011614173228346457 0.023466257668711656 \ No newline at end of file +6 0.29883858267716534 0.7705828220858896 0.011614173228346457 0.023466257668711656 diff --git a/tests/dataset/train/labels/-13-_png.rf.c1d75c2efe355241a72f3c0e873e68e0.txt b/tests/dataset/train/labels/-13-_png.rf.c1d75c2efe355241a72f3c0e873e68e0.txt index ceb4da1..af45a30 100644 --- a/tests/dataset/train/labels/-13-_png.rf.c1d75c2efe355241a72f3c0e873e68e0.txt +++ b/tests/dataset/train/labels/-13-_png.rf.c1d75c2efe355241a72f3c0e873e68e0.txt @@ -4,4 +4,4 @@ 5 0.5962642045454545 0.8931451612903226 0.05821022727272727 0.21370967741935484 2 0.5919176136363636 0.793508064516129 0.019048295454545453 0.03161290322580645 3 0.5923153409090909 0.8534274193548387 0.032883522727272727 0.047903225806451614 -4 0.5963494318181818 0.9377822580645161 0.05730113636363637 0.12443548387096774 \ No newline at end of file +4 0.5963494318181818 0.9377822580645161 0.05730113636363637 0.12443548387096774 diff --git a/tests/dataset/train/labels/-17-_png.rf.3a96bb284b06d743b2641b3e0f2055a9.txt b/tests/dataset/train/labels/-17-_png.rf.3a96bb284b06d743b2641b3e0f2055a9.txt index 72a8769..d9648c0 100644 --- a/tests/dataset/train/labels/-17-_png.rf.3a96bb284b06d743b2641b3e0f2055a9.txt +++ b/tests/dataset/train/labels/-17-_png.rf.3a96bb284b06d743b2641b3e0f2055a9.txt @@ -19,4 +19,4 @@ 6 0.5788210227272728 0.6070625 0.006193181818181819 0.021437499999999998 6 0.5850142045454546 0.6047083333333333 0.005696022727272727 0.022520833333333334 8 0.4936363636363636 0.5785416666666666 0.08048295454545454 0.07264583333333333 -8 0.66 0.441375 0.1435653409090909 0.10529166666666666 \ No newline at end of file +8 0.66 0.441375 0.1435653409090909 0.10529166666666666 diff --git a/tests/dataset/train/labels/-18-_png.rf.80b34a7c8ba57698ddc5d436539b4576.txt b/tests/dataset/train/labels/-18-_png.rf.80b34a7c8ba57698ddc5d436539b4576.txt index ab41260..b3c10d1 100644 --- a/tests/dataset/train/labels/-18-_png.rf.80b34a7c8ba57698ddc5d436539b4576.txt +++ b/tests/dataset/train/labels/-18-_png.rf.80b34a7c8ba57698ddc5d436539b4576.txt @@ -37,4 +37,4 @@ 8 0.13926136363636366 0.21270833333333333 0.16626420454545454 0.42233333333333334 6 0.07434659090909092 0.6906874999999999 0.011051136363636364 0.043895833333333335 6 0.13142045454545453 0.6987916666666667 0.00875 0.036458333333333336 -6 0.1599715909090909 0.7195625 0.01703125 0.04827083333333334 \ No newline at end of file +6 0.1599715909090909 0.7195625 0.01703125 0.04827083333333334 diff --git a/tests/dataset/train/labels/-2-_png.rf.14184d8ea6d551ecb099b7cc25b2ce28.txt b/tests/dataset/train/labels/-2-_png.rf.14184d8ea6d551ecb099b7cc25b2ce28.txt index 36ca869..99ac7d7 100644 --- a/tests/dataset/train/labels/-2-_png.rf.14184d8ea6d551ecb099b7cc25b2ce28.txt +++ b/tests/dataset/train/labels/-2-_png.rf.14184d8ea6d551ecb099b7cc25b2ce28.txt @@ -2,4 +2,4 @@ 6 0.2596022727272727 0.827875 0.01596590909090909 0.045625 6 0.3917897727272727 0.7545000000000001 0.011136363636363637 0.035645833333333335 9 0.6669602272727273 0.492875 0.04575284090909091 0.056479166666666664 -9 0.7556676136363637 0.435 0.06142045454545455 0.051875 \ No newline at end of file +9 0.7556676136363637 0.435 0.06142045454545455 0.051875 diff --git a/tests/dataset/train/labels/-_jpeg.rf.6c9ea300434605e0ac828342b371e204.txt b/tests/dataset/train/labels/-_jpeg.rf.6c9ea300434605e0ac828342b371e204.txt index d2f6f36..f77ee9b 100644 --- a/tests/dataset/train/labels/-_jpeg.rf.6c9ea300434605e0ac828342b371e204.txt +++ b/tests/dataset/train/labels/-_jpeg.rf.6c9ea300434605e0ac828342b371e204.txt @@ -1,4 +1,4 @@ 8 0.7834 0.3682738095238095 0.23556666666666667 0.4511904761904762 8 0.47656666666666664 0.3498214285714286 0.14256666666666667 0.17047619047619048 8 0.6368666666666667 0.3507142857142857 0.12030000000000002 0.13839285714285715 -8 0.5525666666666667 0.6255952380952381 0.06503333333333333 0.10821428571428571 \ No newline at end of file +8 0.5525666666666667 0.6255952380952381 0.06503333333333333 0.10821428571428571 diff --git a/tests/dataset/train/labels/-_jpeg.rf.b4ba444d690bb533133e22f1bd69442d.txt b/tests/dataset/train/labels/-_jpeg.rf.b4ba444d690bb533133e22f1bd69442d.txt index 7a71581..640eba2 100644 --- a/tests/dataset/train/labels/-_jpeg.rf.b4ba444d690bb533133e22f1bd69442d.txt +++ b/tests/dataset/train/labels/-_jpeg.rf.b4ba444d690bb533133e22f1bd69442d.txt @@ -7,4 +7,4 @@ 2 0.616095238095238 0.4248392857142857 0.012833333333333332 0.007089285714285715 5 0.6262380952380951 0.4399821428571428 0.014238095238095239 0.05221428571428571 2 0.6267738095238096 0.4187857142857143 0.013166666666666667 0.00855357142857143 -4 0.627547619047619 0.44232142857142853 0.010059523809523808 0.02155357142857143 \ No newline at end of file +4 0.627547619047619 0.44232142857142853 0.010059523809523808 0.02155357142857143 diff --git a/tests/dataset/val/labels/-_jpeg.rf.3e98d2f5b90e0b1459e15f570a433459.txt b/tests/dataset/val/labels/-_jpeg.rf.3e98d2f5b90e0b1459e15f570a433459.txt index 5f38818..52aabed 100644 --- a/tests/dataset/val/labels/-_jpeg.rf.3e98d2f5b90e0b1459e15f570a433459.txt +++ b/tests/dataset/val/labels/-_jpeg.rf.3e98d2f5b90e0b1459e15f570a433459.txt @@ -32,4 +32,4 @@ 4 0.8660363636363636 0.8001092896174863 0.06530909090909091 0.13043715846994536 4 0.9620727272727273 0.7311475409836066 0.051781818181818186 0.10945355191256831 4 0.03563636363636364 0.8212568306010929 0.06523636363636365 0.09770491803278689 -3 0.03796363636363636 0.7612021857923498 0.034036363636363635 0.022622950819672128 \ No newline at end of file +3 0.03796363636363636 0.7612021857923498 0.034036363636363635 0.022622950819672128 diff --git a/tests/src/utils_test.py b/tests/src/utils_test.py index 99e1788..a112a11 100644 --- a/tests/src/utils_test.py +++ b/tests/src/utils_test.py @@ -1,8 +1,17 @@ +from __future__ import annotations + import unittest -from unittest.mock import MagicMock, patch -from datetime import datetime, timedelta +from datetime import datetime +from datetime import timedelta +from unittest.mock import MagicMock +from unittest.mock import patch + from watchdog.events import FileModifiedEvent -from src.utils import RedisManager, Utils, FileEventHandler + +from src.utils import FileEventHandler +from src.utils import RedisManager +from src.utils import Utils + class TestUtils(unittest.TestCase): def test_is_expired_with_valid_date(self): @@ -18,6 +27,7 @@ def test_is_expired_with_none(self): # Test with None (should return False) self.assertFalse(Utils.is_expired(None)) + class TestFileEventHandler(unittest.TestCase): def test_on_modified_triggers_callback(self): # Create a mock callback function @@ -27,7 +37,9 @@ def test_on_modified_triggers_callback(self): file_path = '/path/to/test/file.txt' # Create an instance of FileEventHandler - event_handler = FileEventHandler(file_path=file_path, callback=mock_callback) + event_handler = FileEventHandler( + file_path=file_path, callback=mock_callback, + ) # Create a mock event for a file modification event = FileModifiedEvent(file_path) @@ -46,7 +58,9 @@ def test_on_modified_does_not_trigger_callback_for_different_file(self): file_path = '/path/to/test/file.txt' # Create an instance of FileEventHandler - event_handler = FileEventHandler(file_path=file_path, callback=mock_callback) + event_handler = FileEventHandler( + file_path=file_path, callback=mock_callback, + ) # Create a mock event for a different file modification different_file_path = '/path/to/other/file.txt' @@ -58,6 +72,7 @@ def test_on_modified_does_not_trigger_callback_for_different_file(self): # Assert that the callback was not called mock_callback.assert_not_called() + class TestRedisManager(unittest.TestCase): """ Test cases for the RedisManager class @@ -93,7 +108,7 @@ def test_set_error(self): """ key = 'test_key' value = b'test_value' - self.mock_redis_instance.set.side_effect = Exception("Redis error") + self.mock_redis_instance.set.side_effect = Exception('Redis error') # Call the set method and verify it handles the exception with self.assertLogs(level='ERROR'): @@ -121,7 +136,7 @@ def test_get_error(self): Simulate an exception during the Redis get operation """ key = 'test_key' - self.mock_redis_instance.get.side_effect = Exception("Redis error") + self.mock_redis_instance.get.side_effect = Exception('Redis error') # Call the get method and verify it handles the exception with self.assertLogs(level='ERROR'): @@ -137,7 +152,8 @@ def test_delete_success(self): # Call the delete method self.redis_manager.delete(key) - # Assert that the Redis delete method was called with correct parameters + # Assert that the Redis delete method + # was called with correct parameters self.mock_redis_instance.delete.assert_called_once_with(key) def test_delete_error(self): @@ -145,7 +161,7 @@ def test_delete_error(self): Simulate an exception during the Redis delete operation """ key = 'test_key' - self.mock_redis_instance.delete.side_effect = Exception("Redis error") + self.mock_redis_instance.delete.side_effect = Exception('Redis error') # Call the delete method and verify it handles the exception with self.assertLogs(level='ERROR'):