Skip to content

Commit 4a4d884

Browse files
committed
Merge pull request #102 from fau-fablab/refactor-code-smells-and-unnecessary-complexity
Refactor code smells and unnecessary complexity
2 parents 3f9ce38 + a6c1039 commit 4a4d884

19 files changed

+69
-72
lines changed

FabLabKasse/UI/ClientDialogCode.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323

2424
class SelectClientDialog(QtGui.QDialog, Ui_SelectClientDialog):
25+
2526
""" GUI code for the dialog for selecting clients """
2627

2728
def __init__(self, parent, shopping_backend):
2829
""" constructor for the ClientDialog
30+
2931
:param parent: parent GUI dialog
3032
:type parent: QtGui.QDialog
3133
:param shopping_backend: current instance of ShoppingBackend
@@ -76,12 +78,12 @@ def insertIntoLineEdit(self, char):
7678
def backspaceLineEdit(self):
7779
if self.lineEdit_pin.hasFocus():
7880
oldtext = self.lineEdit_pin.text()
79-
if len(oldtext) > 0:
81+
if oldtext:
8082
self.lineEdit_pin.setText(oldtext[:-1])
8183
self.lineEditPINUpdate()
8284
else:
8385
oldtext = self.lineEdit_client.text()
84-
if len(oldtext) > 0:
86+
if oldtext:
8587
self.lineEdit_client.setText(oldtext[:-1])
8688
self.lineEditClientUpdate()
8789

@@ -99,7 +101,7 @@ def lineEditClientUpdate(self):
99101

100102
# Check if client number existst and switch to client in comboBox_client
101103
client = self.getClient()
102-
if client == None:
104+
if client is None:
103105
self.comboBox_client.setCurrentIndex(0)
104106
return
105107

FabLabKasse/UI/LoadFromMobileAppDialogCode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, parent, app_url):
5757
QtCore.QTimer.singleShot(0, lambda: self.setWindowState(QtCore.Qt.WindowMaximized))
5858
set_layout_items_visible(self.verticalLayout_app_download, False)
5959
self.pushButton_app.clicked.connect(self._show_app_download)
60-
if app_url == None:
60+
if app_url is None:
6161
self.pushButton_app.setVisible(False)
6262
else:
6363
LoadFromMobileAppDialog.set_qr_label(self.label_qr_app, app_url)

FabLabKasse/UI/PayupCashDialogCode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def showSuggestedDonations(self):
126126
pass
127127
# .disconnect() returns TypeError if currently nothing is connected to the signal
128128
# http://stackoverflow.com/questions/21586643/pyqt-widget-connect-and-disconnect/21587045#21587045
129-
if len(suggestedDonations) > 0:
129+
if suggestedDonations:
130130
donationValue = suggestedDonations.pop()
131131
# functools.partial is used here instead of lambda because "donationValue" needs to be evaluated here.
132132
# when using a lambda, it would be wrongly used by-reference and get the value of the last iteration of this loop
@@ -182,7 +182,7 @@ def update(self):
182182
self.state = "initCanPayout"
183183
elif self.state == "initCanPayout":
184184
pay = p.canPayout()
185-
if pay != None:
185+
if pay is not None:
186186
[self.payoutMaximumAmount, self.payoutRemainingAmount] = pay
187187

188188
# all amounts under 50€ may be paid with <= n+50€

FabLabKasse/UI/PayupManualDialogCode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def insertIntoLineEdit(self, char):
6565

6666
def backspaceLineEdit(self):
6767
oldtext = self.lineEdit.text()
68-
if len(oldtext) > 0:
68+
if oldtext:
6969
self.lineEdit.setText(oldtext[:-3])
7070
self.lineEditUpdated()
7171

FabLabKasse/cashPayment/client/PaymentDeviceClient.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def poll(self):
130130
cmd = None
131131
else:
132132
raise Exception("unknown status")
133-
if cmd == None:
133+
if cmd is None:
134134
return
135135
print "SEND CMD: " + cmd # +"\n"
136136
self.process.write(cmd + "\n")
@@ -139,10 +139,10 @@ def poll(self):
139139
self.lastResponseTime = monotonic_time.monotonic() # get monotonic time. until python 3.3 we have to use this extra module because time.monotonic() is not available in older versions.
140140

141141
response = self.process.readline()
142-
if response == None and self.waitingForResponse \
142+
if response is None and self.waitingForResponse \
143143
and monotonic_time.monotonic() - self.lastResponseTime > 50:
144144
raise Exception("device {} server timeout (>50sec)".format(self))
145-
if response != None:
145+
if response is not None:
146146
print "got response: '" + response + "'"
147147
assert self.waitingForResponse
148148
self.waitingForResponse = False

FabLabKasse/cashPayment/client/PaymentDevicesManager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _canPayout_total(self, canPayoutAmounts):
181181
:return: [totalMaximumRequest, totalRemaining]
182182
"""
183183

184-
if len(canPayoutAmounts) == 0:
184+
if not canPayoutAmounts:
185185
return [0, 0]
186186
[totalMaximumRequest, totalRemaining] = canPayoutAmounts[0]
187187
for [maximumRequest, remainingAmount] in canPayoutAmounts[1:]:
@@ -298,7 +298,7 @@ def formatCent(x):
298298
requested = self.requestedPayin
299299
elif self.mode.startswith("payout"):
300300
requested = self.requestedPayout
301-
if requested != None:
301+
if requested is not None:
302302
text += u":\n{} von {} ".format(formatCent(totalSum), formatCent(requested))
303303
if self.mode.startswith("payin"):
304304
text += "bezahlt (maximal " + formatCent(self.maximumPayin) + ")"
@@ -418,23 +418,23 @@ def wait():
418418
while p.startingUp():
419419
wait()
420420
pay = None
421-
while pay == None:
421+
while pay is None:
422422
wait()
423423
pay = p.canPayout()
424424
print pay
425425
print "Es dürfen maximal {} Cent gezahlt werden. Ein Rest-Rückgeld von unter {} Cent wird nicht zurückgegeben!".format(pay[0], pay[1])
426426
shouldPay = 4213
427427
p.payin(shouldPay, shouldPay + pay[0])
428428
received = None
429-
while received == None:
429+
while received is None:
430430
received = p.getFinalAmount()
431431
wait()
432432
print "Geld erhalten: {}".format(received)
433433
p.poll()
434434
if received > shouldPay:
435435
p.payout(received - shouldPay)
436436
paidOut = None
437-
while paidOut == None:
437+
while paidOut is None:
438438
paidOut = p.getFinalAmount()
439439
wait()
440440
paidOut = -paidOut

FabLabKasse/cashPayment/server/NV11/NV11Device.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ def flushRead(self):
233233
while self.ser.inWaiting() > 0:
234234
self.warn("flushing serial read buffer")
235235
self.ser.read(self.ser.inWaiting())
236-
if len(self.rawBuffer) > 0:
236+
if self.rawBuffer:
237237
self.warn("flushing raw input buffer")
238238
self.rawBuffer = []
239-
if len(self.buffer) > 0:
239+
if self.buffer:
240240
self.warn("flushing input buffer")
241241
self.buffer = []
242242

@@ -254,7 +254,7 @@ def read(self):
254254

255255
# read bytes for de-stuffing
256256

257-
while len(self.rawBuffer) > 0:
257+
while self.rawBuffer:
258258
if self.rawBuffer[0] == 0x7F:
259259
# the STX byte needs special care (byte-stuffing needs to be reversed)
260260
if len(self.rawBuffer) == 1:
@@ -283,7 +283,7 @@ def read(self):
283283

284284
# attention, python subranging is a bit strange: array[a:b] returns array[a] ... array[b-1] (not including b!)
285285

286-
while len(self.buffer) > 0:
286+
while self.buffer:
287287
self.debug2("buffer: " + hex(self.buffer))
288288

289289
# check validity of packet
@@ -350,7 +350,7 @@ class Response(object):
350350
}
351351

352352
def __init__(self, data):
353-
if len(data) == 0:
353+
if not data:
354354
# empty response AFTER decoding
355355
self.status = -1
356356
self.data = []

FabLabKasse/cashPayment/server/exampleServer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# The text of the license conditions can be read at
2020
# <http://www.gnu.org/licenses/>.
2121

22-
from .cashServer import *
22+
from .cashServer import CashServer
2323
import random
2424

2525

FabLabKasse/cashPayment/server/helpers/banknote_stack_helper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _would_stack_from_payout(self, payout_stack, requested_payout):
5757
True if it might be helpful. The driver should do further checks if it is really necessary.
5858
:rtype: bool
5959
"""
60-
if len(payout_stack) == 0:
60+
if not payout_stack:
6161
# no notes to payout
6262
return False
6363
if requested_payout < min(payout_stack):
@@ -82,7 +82,7 @@ def _simulate_simple_payout(self, payout_stack, requested_payout):
8282
"""
8383
simulated_payout = 0
8484
payout_stack = copy.copy(payout_stack)
85-
while len(payout_stack) > 0:
85+
while payout_stack:
8686
if simulated_payout + payout_stack[-1] <= requested_payout:
8787
# would pay out note, if it is not too large
8888
simulated_payout += payout_stack[-1]
@@ -130,7 +130,7 @@ def get_next_payout_action(self, payout_stack, requested_payout):
130130
"""which action should be taken next?
131131
(see the documentation for BanknoteStackHelper for more context information)
132132
"""
133-
if len(payout_stack) == 0:
133+
if not payout_stack:
134134
return "stop"
135135
if self._forced_stacking_is_helpful(payout_stack, requested_payout) and self._would_stack_from_payout(payout_stack, requested_payout):
136136
return "stack"
@@ -145,7 +145,7 @@ def get_next_payout_action(self, payout_stack, requested_payout):
145145

146146
def can_payout(self, payout_stack):
147147
""" implementation for CashServer.getCanPayout()"""
148-
if len(payout_stack) == 0:
148+
if not payout_stack:
149149
# no notes available at all
150150
return self.accepted_rest
151151

@@ -210,7 +210,7 @@ def unittest_payout_forced_stacking(self, random_generator):
210210
origpayout_stack = copy.deepcopy(payout_stack)
211211

212212
simulated_payout = 0
213-
while len(payout_stack) > 0:
213+
while payout_stack:
214214
if self._forced_stacking_is_helpful(payout_stack, requested_payout):
215215
pass
216216
# print("extra stack away useful")

FabLabKasse/cashPayment/server/mdbCash/mdb.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def flushRead(self):
120120
while self.ser.inWaiting() > 0:
121121
self.warn("flushing serial read buffer")
122122
self.ser.read(self.ser.inWaiting())
123-
if len(self.buffer) > 0:
123+
if self.buffer:
124124
self.warn("flushing input buffer")
125125
self.buffer = ""
126126

@@ -144,7 +144,7 @@ def read(self):
144144
else:
145145
pass
146146
# logging.debug("not read:" + self.ser.read())
147-
if len(self.buffer) > 0:
147+
if self.buffer:
148148
logging.debug("buffer: {}".format(self.buffer.__repr__()))
149149
if not ("\n" in self.buffer):
150150
# we have not yet received a full response
@@ -187,7 +187,7 @@ def checksum(self, data):
187187
# raises BusError or InterfaceHardwareError, see send()
188188
# returns valid data
189189
def cmd(self, command, data=None):
190-
if data == None:
190+
if data is None:
191191
data = []
192192
assert 0 <= command < 8
193193
bytes = [self.addr << 3 | command] + data
@@ -203,11 +203,11 @@ def cmd(self, command, data=None):
203203
# timeout for interface board: 1sec
204204
time.sleep(0.1)
205205
resp = self.read() # possibly raises BusError (will be caught below) or InterfaceHardwareError (will not be caught)
206-
if resp != None:
206+
if resp is not None:
207207
resp = resp.strip()
208208
logging.debug("resp: " + resp.__repr__())
209209
break
210-
if resp == None:
210+
if resp is None:
211211
raise InterfaceHardwareError("interface timeout")
212212
else:
213213
break
@@ -216,7 +216,7 @@ def cmd(self, command, data=None):
216216
logging.debug("bus error, resending")
217217
time.sleep(1)
218218
continue
219-
if resp == None:
219+
if resp is None:
220220
raise BusError("no successful response after 3 retries")
221221
responseData = []
222222
# convert response (hex-string) to a byte array
@@ -242,7 +242,7 @@ def extensionCmd(self, data):
242242
# timeout for interface board: 1sec
243243
time.sleep(0.1)
244244
resp = self.read() # possibly raises BusError or InterfaceHardwareError (both will not be caught)
245-
if resp != None:
245+
if resp is not None:
246246
resp = resp.strip()
247247
logging.debug("resp: " + resp.__repr__())
248248
return resp
@@ -313,7 +313,7 @@ def getBits(byte, lowest, highest): # cut out bits lowest...highest (including
313313
status = {"manuallyDispensed": [], "accepted": [], "busy": False}
314314
if data == [MdbCashDevice.ACK]:
315315
return status
316-
while len(data) > 0:
316+
while data:
317317
# parse status response
318318
if data[0] & 1 << 7:
319319
# coin dispensed because of MANUAL! REQUEST (by pressing the button at the changer device itself)
@@ -510,15 +510,15 @@ def _getPossiblePayout(v, t):
510510
# go through the coins from large to small
511511
for [index, value] in v:
512512
n = t[index]["count"]
513-
if previousCoinValue != None and n * value < previousCoinValue:
513+
if previousCoinValue is not None and n * value < previousCoinValue:
514514
# we dont have enough of this coin to "split" one previous coin
515515
continue
516-
if previousCoinValue == None:
516+
if previousCoinValue is None:
517517
previousCoinValue = 0
518518
totalAmount += n * value - previousCoinValue
519519
previousCoinValue = value
520520

521-
if previousCoinValue == None:
521+
if previousCoinValue is None:
522522
return [0, 0]
523523

524524
return [totalAmount, previousCoinValue]

0 commit comments

Comments
 (0)