Skip to content

Commit 34469fd

Browse files
committed
Minor simplifications
1 parent 0ecd9f5 commit 34469fd

File tree

7 files changed

+27
-54
lines changed

7 files changed

+27
-54
lines changed

webware/Examples/AjaxPage.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ def __str__(self):
4141
def __call__(self, *args, **kw):
4242
args = ','.join(map(quoteJs, args))
4343
kwArgs = ','.join(f'{k}={quoteJs(v)}' for k, v in kw.items())
44-
if args and kwArgs:
45-
allArgs = f'{args},{kwArgs}'
46-
elif not kwArgs:
47-
allArgs = args
48-
elif not args:
49-
allArgs = kwArgs
44+
allArgs = f'{args},{kwArgs}' if args and kwArgs else args or kwArgs
5045
return self.__class__(f'{self}({allArgs}')
5146

5247
def __getitem__(self, index):

webware/MiscUtils/DictForArgs.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
import re
99

10+
verbose = False
11+
12+
_nameRE = re.compile(r'\w+')
13+
_equalsRE = re.compile(r'\=')
14+
_stringRE = re.compile(r'''"[^"]+"|'[^']+'|\S+''')
15+
_whiteRE = re.compile(r'\s+')
16+
17+
_REs = {_nameRE: 'name', _equalsRE: 'equals',
18+
_stringRE: 'string', _whiteRE: 'white'}
19+
1020

1121
class DictForArgsError(Exception):
1222
"""Error when building dictionary from arguments."""
@@ -16,14 +26,6 @@ def _SyntaxError(s):
1626
raise DictForArgsError(f'Syntax error: {s!r}')
1727

1828

19-
_nameRE = re.compile(r'\w+')
20-
_equalsRE = re.compile(r'\=')
21-
_stringRE = re.compile(r'''"[^"]+"|'[^']+'|\S+''')
22-
_whiteRE = re.compile(r'\s+')
23-
24-
_REs = [_nameRE, _equalsRE, _stringRE, _whiteRE]
25-
26-
2729
def dictForArgs(s):
2830
"""Build dictionary from arguments.
2931
@@ -58,7 +60,6 @@ def dictForArgs(s):
5860

5961
# Tokenize
6062

61-
verbose = False
6263
matches = []
6364
start = 0
6465
sLen = len(s)
@@ -84,17 +85,7 @@ def dictForArgs(s):
8485
_SyntaxError(s)
8586

8687
if verbose:
87-
names = []
88-
for match in matches:
89-
if match.re is _nameRE:
90-
name = 'name'
91-
elif match.re is _equalsRE:
92-
name = 'equals'
93-
elif match.re is _stringRE:
94-
name = 'string'
95-
elif match.re is _whiteRE:
96-
name = 'white'
97-
names.append(name)
88+
names = ', '.join(_REs[match.re] for match in matches)
9889
print('>> names =', names)
9990

10091
# Process tokens
@@ -107,10 +98,7 @@ def dictForArgs(s):
10798
i = 0
10899
while i < matchesLen:
109100
match = matches[i]
110-
if i + 1 < matchesLen:
111-
peekMatch = matches[i+1]
112-
else:
113-
peekMatch = None
101+
peekMatch = matches[i+1] if i + 1 < matchesLen else None
114102
if match.re is _nameRE:
115103
if peekMatch is not None:
116104
if peekMatch.re is _nameRE:
@@ -121,9 +109,9 @@ def dictForArgs(s):
121109
if peekMatch.re is _equalsRE:
122110
if i + 2 < matchesLen:
123111
target = matches[i+2]
124-
if target.re is _nameRE or target.re is _stringRE:
112+
if target.re in (_nameRE, _stringRE):
125113
value = target.group()
126-
if value[0] == "'" or value[0] == '"':
114+
if value[0] in ("'", '"'):
127115
value = value[1:-1]
128116
d[match.group()] = value
129117
i += 3

webware/MiscUtils/Tests/TestCSVParser.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ def testNegatives(self):
2121
'a\n,b'
2222
]
2323
for inp in inputs:
24-
try:
24+
with self.assertRaises(
25+
ParseError, msg=f'Did not get an exception for: {inp!r}'):
2526
results = self.parse(inp)
26-
except ParseError:
27-
pass
28-
else:
29-
print()
30-
print('results:', repr(results))
31-
raise Exception(f'Did not get an exception for: {inp!r}')
27+
print(f'\nresults: {results!r}')
3228

3329
def testPositives(self):
3430
tests = [
@@ -86,19 +82,16 @@ def testPositives(self):
8682
self.assertEqual(
8783
res, out,
8884
f'\ninput={inp!r}\nresult={res!r}\noutput={out!r}')
89-
res = self.parse(inp+'\n')
85+
res = self.parse(inp + '\n')
9086
self.assertEqual(
9187
res, out,
9288
f'\ninput={inp!r}\nresult={res!r}\noutput={out!r}')
9389
else:
9490
# multiple lines
95-
gotFields = False
91+
res = None
9692
for line in inp.splitlines():
97-
self.assertFalse(gotFields)
93+
self.assertIsNone(res)
9894
res = self.parse(line)
99-
if res is not None:
100-
gotFields = True
101-
self.assertTrue(gotFields)
10295
self.assertEqual(
10396
res, out,
10497
f'\ninput={inp!r}\nresult={res!r}\noutput={out!r}')

webware/MiscUtils/Tests/TestDataTable.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,10 @@ def testBasics(self):
212212
self._testSource('MK enums', src, headings, data)
213213

214214
# Unfinished multiline record
215-
try:
215+
with self.assertRaises(
216+
DataTableError,
217+
msg='Did not raise error for unfinished multiline record'):
216218
DataTable().readString('a\n"1\n')
217-
except DataTableError:
218-
pass # just what we were expecting
219-
else:
220-
raise Exception(
221-
'Failed to raise exception for unfinished multiline record')
222219

223220
def testDefaultUsePickleCache(self):
224221
t = DataTable()

webware/PSP/BraceConverter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def parseLine(self, line, writer):
7777
self.line = ''
7878
else:
7979
# should never get here
80-
raise Exception()
80+
raise ValueError(f'Invalid character: {c!r}')
8181
writer.printChars('\n')
8282

8383
def openBlock(self, writer):

webware/Testing/Main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ def writeNotes(self):
9292
</ul>''')
9393

9494
def error(self, msg):
95-
raise Exception(msg)
95+
raise RuntimeError(msg)

webware/Tests/TestSessions/Application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setting(self, key, default=None):
2222
}.get(key, default)
2323

2424
def handleException(self):
25-
raise Exception('Application Error')
25+
raise RuntimeError('Application Error')
2626

2727
def sessionTimeout(self, _trans=None):
2828
return self._sessionTimeout

0 commit comments

Comments
 (0)