Skip to content

Commit 5ffe939

Browse files
committed
refactor: sorted imports
1 parent b130463 commit 5ffe939

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

postgresql_watcher/watcher.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import Optional, Callable
2-
from psycopg2 import connect, extensions
1+
from logging import Logger, getLogger
32
from multiprocessing import Process, Pipe
43
from multiprocessing.connection import Connection
5-
import time
6-
from select import select
7-
from logging import Logger, getLogger
4+
from time import sleep, time
5+
from typing import Optional, Callable
6+
7+
from psycopg2 import connect, extensions
8+
89
from .casbin_channel_subscription import (
910
casbin_channel_subscription,
1011
_ChannelSubscriptionMessage,
@@ -115,7 +116,7 @@ def start(self):
115116
message = int(self.parent_conn.recv())
116117
if message == _ChannelSubscriptionMessage.IS_READY:
117118
break
118-
time.sleep(1 / 1000) # wait for 1 ms
119+
sleep(1 / 1000) # wait for 1 ms
119120

120121
def _cleanup_connections_and_processes(self) -> None:
121122
# Clean up potentially existing Connections and Processes
@@ -155,9 +156,7 @@ def update(self) -> None:
155156
# Can only receive notifications when not in transaction, set this for easier usage
156157
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
157158
curs = conn.cursor()
158-
curs.execute(
159-
f"NOTIFY {self.channel_name},'casbin policy update at {time.time()}'"
160-
)
159+
curs.execute(f"NOTIFY {self.channel_name},'casbin policy update at {time()}'")
161160
conn.close()
162161

163162
def should_reload(self) -> bool:

tests/test_postgresql_watcher.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import sys
2-
import unittest
3-
from unittest.mock import MagicMock
4-
from multiprocessing.connection import Pipe
1+
from logging import DEBUG, getLogger, StreamHandler
2+
from multiprocessing import connection, context
53
from time import sleep
6-
import logging
4+
from unittest import TestCase, main
5+
from unittest.mock import MagicMock
6+
import sys
77

88
from postgresql_watcher import PostgresqlWatcher
99
from postgresql_watcher.casbin_channel_subscription import CASBIN_CHANNEL_SELECT_TIMEOUT
10-
from multiprocessing import connection, context
10+
1111

1212
# Warning!!! , Please setup yourself config
1313
HOST = "127.0.0.1"
@@ -16,9 +16,9 @@
1616
PASSWORD = "123456"
1717
DBNAME = "postgres"
1818

19-
logger = logging.getLogger()
20-
logger.level = logging.DEBUG
21-
stream_handler = logging.StreamHandler(sys.stdout)
19+
logger = getLogger()
20+
logger.level = DEBUG
21+
stream_handler = StreamHandler(sys.stdout)
2222
logger.addHandler(stream_handler)
2323

2424

@@ -43,7 +43,7 @@ def get_watcher(channel_name):
4343
_winapi = None
4444

4545

46-
class TestConfig(unittest.TestCase):
46+
class TestConfig(TestCase):
4747
def test_pg_watcher_init(self):
4848
pg_watcher = get_watcher("test_pg_watcher_init")
4949
if _winapi:
@@ -104,4 +104,4 @@ def test_update_handler_not_called(self):
104104

105105

106106
if __name__ == "__main__":
107-
unittest.main()
107+
main()

0 commit comments

Comments
 (0)