Skip to content

Commit 1e1bf73

Browse files
committed
Use more modern Python idioms
1 parent 1f57f22 commit 1e1bf73

File tree

8 files changed

+29
-27
lines changed

8 files changed

+29
-27
lines changed

DBUtils/Examples/DBUtilsExample.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class DBUtilsExample(ExamplePage):
6363
if dbapi_name != 'pg':
6464
config['creator'] = dbapi
6565
dbclass = getattr(dbmod, dbmod_name)(**config)
66-
except dbapi.Error, error:
66+
except dbapi.Error as error:
6767
dbstatus = str(error)
6868
except Exception:
6969
dbstatus = 'Could not connect to the database.'
@@ -107,7 +107,7 @@ def output(self, s):
107107

108108
def outputMsg(self, msg, error=False):
109109
self._output.append('<p style="color:%s">%s</p>'
110-
% (error and 'red' or 'green', msg))
110+
% ('red' if error else 'green', msg))
111111

112112
def connection(self, shareable=True):
113113
if self.dbstatus:
@@ -118,7 +118,7 @@ def connection(self, shareable=True):
118118
return self.dbclass.connection(shareable)
119119
else:
120120
return self.dbclass.connection()
121-
except self.dbapi.Error, error:
121+
except self.dbapi.Error as error:
122122
error = str(error)
123123
except Exception:
124124
error = 'Cannot connect to the database.'
@@ -288,7 +288,7 @@ def listAttendees(self):
288288
'<tr><th>Name</th><th>Seminar</th><th>Paid</th>'
289289
'<th>%s</th></tr>' % button)
290290
for name, id, title, paid in result:
291-
paid = paid and 'Yes' or 'No'
291+
paid = 'Yes' if paid else 'No'
292292
id = id.ljust(4) + name
293293
wr('<tr><td>%s</td><td>%s</td><td>%s</td>'
294294
'<td><input type="checkbox" name="id" value="%s"></td>'

DBUtils/SteadyDB.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def __init__(self, creator, maxusage=None, setsession=None,
188188
failures, tuple) and not issubclass(failures, Exception):
189189
raise TypeError("'failures' must be a tuple of exceptions.")
190190
self._failures = failures
191-
self._ping = isinstance(ping, int) and ping or 0
191+
self._ping = ping if isinstance(ping, int) else 0
192192
self._closeable = closeable
193193
self._args, self._kwargs = args, kwargs
194194
self._store(self._create())
@@ -667,7 +667,7 @@ def tough_method(*args, **kwargs):
667667
def __getattr__(self, name):
668668
"""Inherit methods and attributes of underlying cursor."""
669669
if self._cursor:
670-
if name.startswith('execute') or name.startswith('call'):
670+
if name.startswith(('execute', 'call')):
671671
# make execution methods "tough"
672672
return self._get_tough_method(name)
673673
else:

DBUtils/Tests/TestPooledDB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def test09_SortShared(self):
514514
db.cursor().execute('select test')
515515
cache.append(db)
516516
for i, db in enumerate(cache):
517-
self.assertEqual(db._shared_con.shared, 2 <= i < 4 and 1 or 2)
517+
self.assertEqual(db._shared_con.shared, 1 if 2 <= i < 4 else 2)
518518
cache[2].begin()
519519
cache[3].begin()
520520
db = pool.connection()

DBUtils/Tests/TestSimplePooledDB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def cursor(self):
4949
def versionString(version):
5050
"""Create version string."""
5151
ver = [str(v) for v in version]
52-
numbers, rest = ver[:ver[2] == '0' and 2 or 3], ver[3:]
52+
numbers, rest = ver[:2 if ver[2] == '0' else 3], ver[3:]
5353
return '.'.join(numbers) + '-'.join(rest)
5454

5555

DBUtils/Tests/TestSteadyDB.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,10 @@ def test06_ConnectionMaxUsage(self):
440440
if i == 5:
441441
db._con.valid = cursor._cursor.valid = False
442442
cursor.callproc('test')
443-
j = (i + (i < 5 and 3 or -5)) % 10 + 1
443+
j = (i + (3 if i < 5 else -5)) % 10 + 1
444444
self.assertEqual(db._usage, j)
445445
self.assertEqual(db._con.num_uses, j)
446-
j = i < 5 and 3 or 0
446+
j = 3 if i < 5 else 0
447447
self.assertEqual(db._con.num_queries, j)
448448
db.close()
449449
cursor.execute('select test1')

DBUtils/Tests/TestSteadyPg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ def test5_ConnectionMaxUsage(self):
321321
db.db.status = False
322322
r = db.get_tables()
323323
self.assertEqual(r, 'test')
324-
j = (i + (i < 5 and 3 or -5)) % 10 + 1
324+
j = (i + (3 if i < 5 else -5)) % 10 + 1
325325
self.assertEqual(db._usage, j)
326-
j = i < 5 and 3 or 0
326+
j = 3 if i < 5 else 0
327327
self.assertEqual(db.num_queries, j)
328328
db.close()
329329
self.assertEqual(db.query('select test1'), 'test1')

buildhtml.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
"""Build HMTL from reST files."""
44

5+
from __future__ import print_function
6+
57
from glob import glob
68
from os.path import splitext, join
79
from docutils.core import publish_file
810

9-
print "Creating the documentation..."
11+
print("Creating the documentation...")
1012

1113
for rst_file in glob(join('DBUtils', 'Docs', '*.rst')):
1214
name = splitext(rst_file)[0]
@@ -18,18 +20,18 @@
1820
else:
1921
lang = 'en'
2022
html_file = name + '.html'
21-
print name, lang
23+
print(name, lang)
2224

23-
source = open(rst_file, 'r')
24-
destination=open(html_file, 'w')
25-
publish_file(writer_name='html5',
26-
source=source, destination=destination,
27-
settings_overrides = dict(
28-
stylesheet_path='Doc.css',
29-
embed_stylesheet=False,
30-
toc_backlinks=False,
31-
language_code=lang
32-
)
33-
)
25+
with open(rst_file) as source:
26+
with open(html_file, 'w') as destination:
27+
publish_file(writer_name='html5',
28+
source=source, destination=destination,
29+
settings_overrides = dict(
30+
stylesheet_path='Doc.css',
31+
embed_stylesheet=False,
32+
toc_backlinks=False,
33+
language_code=lang
34+
)
35+
)
3436

35-
print "Done."
37+
print("Done.")

setversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def versionString(version):
3939
4040
"""
4141
ver = map(str, version)
42-
numbers, rest = ver[:ver[2] == '0' and 2 or 3], ver[3:]
42+
numbers, rest = ver[:2 if ver[2] == '0' else 3], ver[3:]
4343
return '.'.join(numbers) + '-'.join(rest)
4444

4545
versionString = versionString(version)

0 commit comments

Comments
 (0)