Skip to content

Commit f338538

Browse files
committed
Simplify control flow
1 parent 6414e00 commit f338538

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

dbutils/pooled_pg.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,11 @@ def cache(self, con):
225225
try:
226226
if self._reset == RESET_COMPLETELY:
227227
con.reset() # reset the connection completely
228-
else:
229-
if self._reset == RESET_ALWAYS_ROLLBACK or con._transaction:
230-
try:
231-
con.rollback() # rollback a possible transaction
232-
except Exception:
233-
pass
228+
elif self._reset == RESET_ALWAYS_ROLLBACK or con._transaction:
229+
try:
230+
con.rollback() # rollback a possible transaction
231+
except Exception:
232+
pass
234233
self._cache.put_nowait(con) # and then put it back into the cache
235234
except Full:
236235
con.close()

dbutils/simple_pooled_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,4 @@ def _threadsafe_return_connection(self, con):
212212
In this case, the connections always stay in the pool,
213213
so there is no need to do anything here.
214214
"""
215-
pass
215+
# we don't need to do anything here

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ select = [
106106
]
107107
# You can use `ruff rule ...` to see what these ignored rules check
108108
ignore = [
109-
"PIE790",
110-
"PLR5501",
111109
"PTH122",
112110
"PTH123",
113111
"S110",

tests/test_persistent_db.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,16 @@ def run_queries(idx):
8484
db = persist.connection()
8585
if db != this_db:
8686
res = 'error - not persistent'
87+
elif q == 'ping':
88+
res = 'ok - thread alive'
89+
elif q == 'close':
90+
db.close()
91+
res = 'ok - connection closed'
8792
else:
88-
if q == 'ping':
89-
res = 'ok - thread alive'
90-
elif q == 'close':
91-
db.close()
92-
res = 'ok - connection closed'
93-
else:
94-
cursor = db.cursor()
95-
cursor.execute(q)
96-
res = cursor.fetchone()
97-
cursor.close()
93+
cursor = db.cursor()
94+
cursor.execute(q)
95+
res = cursor.fetchone()
96+
cursor.close()
9897
res = f'{idx}({db._usage}): {res}'
9998
result_queue[idx].put(res, timeout=1)
10099
if db:

tests/test_persistent_pg.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ def run_queries(idx):
6262
db = persist.connection()
6363
if db.db != this_db:
6464
res = 'error - not persistent'
65+
elif q == 'ping':
66+
res = 'ok - thread alive'
67+
elif q == 'close':
68+
db.db.close()
69+
res = 'ok - connection closed'
6570
else:
66-
if q == 'ping':
67-
res = 'ok - thread alive'
68-
elif q == 'close':
69-
db.db.close()
70-
res = 'ok - connection closed'
71-
else:
72-
res = db.query(q)
71+
res = db.query(q)
7372
res = f'{idx}({db._usage}): {res}'
7473
result_queue[idx].put(res, timeout=1)
7574
if db:

0 commit comments

Comments
 (0)