Skip to content

Commit cd95edc

Browse files
committed
Refactor: Attempt to fix Python linting
1 parent 9aedf16 commit cd95edc

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

micropython/examples/badger2040/qrgen.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
# create demo QR code file if no QR code files exist
2020
if len(CODES) == 0:
2121
try:
22-
newQRcodeFilename = "qrcode.txt"
23-
text = open("qrcodes/{}".format(newQRcodeFilename), "w")
24-
text.write("""https://pimoroni.com/badger2040
22+
new_qr_code_filename = "qrcode.txt"
23+
with open(f"/qrcodes/{new_qr_code_filename}", "w") as text:
24+
text.write("""https://pimoroni.com/badger2040
2525
Badger 2040
2626
* 296x128 1-bit e-ink
2727
* six user buttons
@@ -31,13 +31,12 @@
3131
Scan this code to learn
3232
more about Badger 2040.
3333
""")
34-
text.flush()
35-
text.close()
34+
text.flush()
3635

37-
# Set the CODES list to contain the newQRcodeFilename (created above)
38-
CODES = [newQRcodeFilename]
36+
# Set the CODES list to contain the new_qr_code_filename (created above)
37+
CODES = [new_qr_code_filename]
3938

40-
except:
39+
except OSError:
4140
CODES = []
4241

4342
TOTAL_CODES = len(CODES)
@@ -51,18 +50,15 @@
5150
"current_qr": 0
5251
}
5352

53+
5454
def set_state_current_index_in_range():
5555
badger_os.state_load("qrcodes", state)
56-
5756
if state["current_qr"] >= len(CODES):
5857
state["current_qr"] = len(CODES) - 1 # set to last index (zero-based). Note: will set to -1 if currently 0
59-
60-
# check that the index is not negative, thus still out of range
61-
if state["current_qr"] < 0:
58+
if state["current_qr"] < 0: # check that the index is not negative, thus still out of range
6259
state["current_qr"] = 0
63-
6460
badger_os.state_save("qrcodes", state)
65-
61+
6662

6763
def measure_qr_code(size, code):
6864
w, h = code.get_size()
@@ -86,12 +82,9 @@ def draw_qr_file(n):
8682
file = CODES[n]
8783

8884
try:
89-
codetext = open("qrcodes/{}".format(file), "r")
90-
91-
lines = codetext.read().strip().split("\n")
92-
93-
codetext.close()
94-
except:
85+
with open(f"/qrcodes/{file}", "r") as codetext:
86+
lines = codetext.read().strip().split("\n")
87+
except OSError:
9588
lines = ["", "", "", "", "", "", "", "", "", ""]
9689

9790
code_text = lines.pop(0)

0 commit comments

Comments
 (0)