Skip to content

Commit

Permalink
Adjust layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Oct 22, 2024
1 parent 36f33f1 commit a8dd23d
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ API_PASSWORD='passcode'
API_URL = "http://localhost:5000"
redis_host = 'localhost'
redis_port = 6379
redis_password = ''
redis_password = ''
21 changes: 12 additions & 9 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -23,15 +25,16 @@ 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):
"""
Initialises the FileEventHandler instance.
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
Expand All @@ -46,6 +49,7 @@ def on_modified(self, event):
if event.src_path == self.file_path:
self.callback()


class RedisManager:
def __init__(self):
"""
Expand All @@ -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).
Expand All @@ -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.
Expand All @@ -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)}")
logging.error(f"Error deleting Redis key {key}: {str(e)}")
2 changes: 1 addition & 1 deletion tests/dataset/coco_annotations.json
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,4 @@
"supercategory": "none"
}
]
}
}
4 changes: 2 additions & 2 deletions tests/dataset/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
- [[16, 19, 22, 24], 1, Detect, [nc]] # Detect(P2, P3, P4, P5)
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 0.5040168539325843 0.7449397590361445 0.02047752808988764 0.07783132530120482
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 0.29883858267716534 0.7705828220858896 0.011614173228346457 0.023466257668711656
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 0.5963494318181818 0.9377822580645161 0.05730113636363637 0.12443548387096774
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 0.66 0.441375 0.1435653409090909 0.10529166666666666
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 0.1599715909090909 0.7195625 0.01703125 0.04827083333333334
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 0.7556676136363637 0.435 0.06142045454545455 0.051875
Original file line number Diff line number Diff line change
@@ -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
8 0.5525666666666667 0.6255952380952381 0.06503333333333333 0.10821428571428571
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 0.627547619047619 0.44232142857142853 0.010059523809523808 0.02155357142857143
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 0.03796363636363636 0.7612021857923498 0.034036363636363635 0.022622950819672128
34 changes: 25 additions & 9 deletions tests/src/utils_test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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'):
Expand All @@ -137,15 +152,16 @@ 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):
"""
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'):
Expand Down

0 comments on commit a8dd23d

Please sign in to comment.