Skip to content

Commit c9d79f4

Browse files
committed
Use walrus operator
1 parent fb486bf commit c9d79f4

File tree

8 files changed

+16
-35
lines changed

8 files changed

+16
-35
lines changed

webware/MiscUtils/M2PickleRPC.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,15 @@ def make_connection(self, host, port=None):
3434
def parse_response(self, f):
3535
"""Workaround M2Crypto issue mentioned above."""
3636
sio = BytesIO()
37-
while True:
38-
chunk = f.read()
39-
if not chunk:
40-
break
37+
while chunk := f.read():
4138
sio.write(chunk)
4239
sio.seek(0)
4340
return Transport.parse_response(self, sio)
4441

4542
def parse_response_gzip(self, f):
4643
"""Workaround M2Crypto issue mentioned above."""
4744
sio = BytesIO()
48-
while True:
49-
chunk = f.read()
50-
if not chunk:
51-
break
45+
while chunk := f.read():
5246
sio.write(chunk)
5347
sio.seek(0)
5448
return Transport.parse_response_gzip(self, sio)

webware/MiscUtils/MixIn.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def foo(self):
6666

6767
# Track the mix-ins made for a particular class
6868
attrName = 'mixInsFor' + pyClass.__name__
69-
mixIns = getattr(pyClass, attrName, None)
70-
if mixIns is None:
69+
if (mixIns := getattr(pyClass, attrName, None)) is None:
7170
mixIns = []
7271
setattr(pyClass, attrName, mixIns)
7372

webware/MiscUtils/NamedValueAccess.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,10 @@ def valueForKey(obj, key, default=NoDefault):
7575
method = getattr(cls, key, None)
7676
if not method:
7777
underKey = '_' + key
78-
method = getattr(cls, underKey, None) if cls else None
79-
if not method:
80-
attr = getattr(obj, key, NoDefault)
81-
if attr is NoDefault:
82-
attr = getattr(obj, underKey, NoDefault)
83-
if attr is NoDefault and cls is not None:
84-
getitem = getattr(cls, '__getitem__', None)
85-
if getitem:
78+
if not (method := getattr(cls, underKey, None) if cls else None):
79+
if (attr := getattr(obj, key, NoDefault)) is NoDefault:
80+
if (attr := getattr(obj, underKey, NoDefault)) is NoDefault and cls is not None:
81+
if getitem := getattr(cls, '__getitem__', None):
8682
try:
8783
getitem(obj, key)
8884
except KeyError:

webware/PSP/BraceConverter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def parseLine(self, line, writer):
8282

8383
def openBlock(self, writer):
8484
"""Open a new block."""
85-
match = self._reColonBrace.match(self.line)
86-
if match and not self.dictLevel:
85+
if (match := self._reColonBrace.match(self.line)) and not self.dictLevel:
8786
writer.printChars(':')
8887
writer.pushIndent()
8988
if match.group(1):

webware/Tests/TestEndToEnd/TestServer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ def run(self):
120120
encoding='utf-8', stdout=PIPE, stderr=STDOUT) as p:
121121
outputStarted = False
122122
while True:
123-
ret = p.poll()
124-
if ret is not None:
123+
if (ret := p.poll()) is not None:
125124
break
126125
line = p.stdout.readline().rstrip()
127126
if line:
@@ -133,8 +132,7 @@ def run(self):
133132
self.pollQueue.put(ret)
134133
if ret is None:
135134
while True:
136-
ret = p.poll()
137-
if ret is not None:
135+
if (ret := p.poll()) is not None:
138136
break
139137
line = p.stdout.readline().rstrip()
140138
self.outputQueue.put(line)

webware/Transaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ def writeExceptionReport(self, handler):
197197
handler.writeAttrs(self, self._exceptionReportAttrNames)
198198

199199
for name in self._exceptionReportAttrNames:
200-
obj = getattr(self, '_' + name, None)
201-
if obj:
200+
if obj := getattr(self, '_' + name, None):
202201
try:
203202
obj.writeExceptionReport(handler)
204203
except Exception:

webware/URLParser.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ def parse(self, trans, requestPath):
266266
context.append('')
267267
parts = []
268268
while context:
269-
contextName = '/'.join(context)
270-
if contextName in self._contexts:
269+
if (contextName := '/'.join(context)) in self._contexts:
271270
break
272271
parts.insert(0, context.pop())
273272
if context:
@@ -645,7 +644,7 @@ def parseInit(self, trans, requestPath):
645644
redirectTo = mod.urlRedirect['']
646645
else:
647646
redirectTo = None
648-
if redirectTo:
647+
if redirectTo := (mod.urlRedirect.get(nextPart) or mod.urlRedirect.get('')):
649648
if isinstance(redirectTo, str):
650649
fp = FileParser(os.path.join(self._path, redirectTo))
651650
else:

webware/UnknownFileTypeServlet.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def filename(self, trans):
7878
A subclass could override this in order to serve files from other
7979
disk locations based on some logic.
8080
"""
81-
filename = getattr(self, '_serverSideFilename', None)
82-
if filename is None:
81+
if (filename := getattr(self, '_serverSideFilename', None)) is None:
8382
filename = trans.request().serverSidePath()
8483
self._serverSideFilename = filename # cache it
8584
return filename
@@ -199,8 +198,7 @@ def serveContent(self, trans):
199198
print('>> UnknownFileType.serveContent()')
200199
print('>> filename =', filename)
201200
print('>> size=', fileSize)
202-
fileDict = fileCache.get(filename)
203-
if fileDict is not None and mtime != fileDict['mtime']:
201+
if (fileDict := fileCache.get(filename)) is not None and mtime != fileDict['mtime']:
204202
# Cache is out of date; clear it.
205203
if debug:
206204
print('>> changed, clearing cache')
@@ -241,8 +239,7 @@ def serveContent(self, trans):
241239
print('>> sending directly')
242240
numBytesSent = 0
243241
while numBytesSent < fileSize:
244-
data = f.read(min(fileSize-numBytesSent, readBufferSize))
245-
if data == '':
242+
if not (data := f.read(min(fileSize-numBytesSent, readBufferSize))):
246243
break # unlikely, but safety first
247244
response.write(data)
248245
numBytesSent += len(data)

0 commit comments

Comments
 (0)