Skip to content

Commit e45efc6

Browse files
committed
Replace try...finally with contextmanagers
1 parent 02d4fd3 commit e45efc6

File tree

8 files changed

+35
-64
lines changed

8 files changed

+35
-64
lines changed

src/pn532/example/__init__.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"""Example."""
33

44
from argparse import ArgumentParser
5+
from contextlib import contextmanager
6+
7+
from RPi.GPIO import GPIO # noqa: N814
58

69
from pn532 import (
710
PN532_SPI,
@@ -10,19 +13,31 @@
1013
)
1114

1215

13-
def spi():
14-
"""Return default spi device."""
15-
return PN532_SPI(debug=False, reset=20, cs=4)
16+
@contextmanager
17+
def spi(debug=False, reset=20, cs=4):
18+
"""Yield a spi device."""
19+
try:
20+
yield PN532_SPI(debug=debug, reset=reset, cs=cs)
21+
finally:
22+
GPIO.cleanup()
1623

1724

18-
def i2c():
19-
"""Return default i2c device."""
20-
return PN532_I2C(debug=False, reset=20, req=16)
25+
@contextmanager
26+
def i2c(debug=False, reset=20, req=16):
27+
"""Yield a i2c device."""
28+
try:
29+
yield PN532_I2C(debug=debug, reset=reset, req=req)
30+
finally:
31+
GPIO.cleanup()
2132

2233

23-
def uart():
24-
"""Return default uart device."""
25-
return PN532_UART(debug=False, reset=20)
34+
@contextmanager
35+
def uart(debug=False, reset=20):
36+
"""Yield a uart device."""
37+
try:
38+
yield PN532_UART(debug=debug, reset=reset)
39+
finally:
40+
GPIO.cleanup()
2641

2742

2843
def mode(value):

src/pn532/example/gpio/read.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
This example shows connecting to the PN532 and reading the GPIOs.
66
"""
77

8-
import RPi.GPIO as GPIO # noqa: N814
9-
108
from pn532.example import parse_mode
119

1210

1311
def run(mode):
1412
"""Run."""
15-
try:
16-
pn532 = mode()
17-
13+
with mode() as pn532:
1814
ic, ver, rev, support = pn532.get_firmware_version()
1915
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
2016

@@ -29,8 +25,6 @@ def run(mode):
2925
print("i0:", pn532.read_gpio("I0"))
3026
print("i1:", pn532.read_gpio("I1"))
3127
print("P34:", pn532.read_gpio("P34"))
32-
finally:
33-
GPIO.cleanup()
3428

3529

3630
def main():

src/pn532/example/gpio/write.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
This example shows connecting to the PN532 and writing the GPIOs.
66
"""
77

8-
import RPi.GPIO as GPIO # noqa: N814
9-
108
from pn532.example import parse_mode
119

1210

1311
def run(mode):
1412
"""Run."""
15-
try:
16-
pn532 = mode()
17-
13+
with mode() as pn532:
1814
ic, ver, rev, support = pn532.get_firmware_version()
1915
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
2016

@@ -50,8 +46,6 @@ def run(mode):
5046
)
5147
print("2. P71/P72 are always HIGH in SPI mode.")
5248
print("3. DO NOT reset the P32 and P34 pins.")
53-
finally:
54-
GPIO.cleanup()
5549

5650

5751
def main():

src/pn532/example/mifare/dump.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
type RFID tag
77
"""
88

9-
import RPi.GPIO as GPIO # noqa: N814
10-
119
import pn532.pn532 as nfc
1210

1311
from pn532.example import parse_mode
1412

1513

1614
def run(mode):
1715
"""Run."""
18-
try:
19-
pn532 = mode()
20-
16+
with mode() as pn532:
2117
ic, ver, rev, support = pn532.get_firmware_version()
2218
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
2319

@@ -57,8 +53,6 @@ def run(mode):
5753
except nfc.PN532Error as e:
5854
print(e.errmsg)
5955
break
60-
finally:
61-
GPIO.cleanup()
6256

6357

6458
def main():

src/pn532/example/mifare/rw.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""
3+
Read/write mifare.
4+
35
This example shows connecting to the PN532 and writing an M1
46
type RFID tag
57
@@ -13,17 +15,13 @@
1315
2. Block 0 is unwritable.
1416
"""
1517

16-
import RPi.GPIO as GPIO # noqa: N814
17-
1818
import pn532.pn532 as nfc
1919
from pn532.example import parse_mode
2020

2121

2222
def run(mode):
2323
"""Run."""
24-
try:
25-
pn532 = mode()
26-
24+
with mode() as pn532:
2725
ic, ver, rev, support = pn532.get_firmware_version()
2826
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
2927

@@ -86,8 +84,6 @@ def run(mode):
8684
print("write block %d successfully" % block_number)
8785
except nfc.PN532Error as e:
8886
print(e.errmsg)
89-
finally:
90-
GPIO.cleanup()
9187

9288

9389
def main():

src/pn532/example/ntag2/dump.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66
type RFID tag
77
"""
88

9-
import RPi.GPIO as GPIO # noqa: N814
10-
119
import pn532.pn532 as nfc
1210
from pn532.example import parse_mode
1311

1412

1513
def run(mode):
1614
"""Run."""
17-
try:
18-
pn532 = mode()
19-
15+
with mode() as pn532:
2016
ic, ver, rev, support = pn532.get_firmware_version()
21-
print(
22-
" Found PN532 with firmware version: {0}.{1}".format(ver, rev)
23-
)
17+
print(f"Found PN532 with firmware version: {ver}.{rev}")
2418

2519
# Configure PN532 to communicate with NTAG215 cards
2620
pn532.SAM_configuration()
@@ -48,8 +42,6 @@ def run(mode):
4842
except nfc.PN532Error as e:
4943
print(e.errmsg)
5044
break
51-
finally:
52-
GPIO.cleanup()
5345

5446

5547
def main():

src/pn532/example/ntag2/rw.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
type RFID tag
77
"""
88

9-
import RPi.GPIO as GPIO # noqa: N814
10-
119
import pn532.pn532 as nfc
1210

1311
from pn532.example import parse_mode
1412

1513

1614
def run(mode):
1715
"""Run."""
18-
try:
19-
pn532 = mode()
20-
16+
with mode() as pn532:
2117
ic, ver, rev, support = pn532.get_firmware_version()
22-
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
18+
print("Found PN532 with firmware version: {ver}.{rev}")
2319

2420
# Configure PN532 to communicate with NTAG215 cards
2521
pn532.SAM_configuration()
@@ -44,8 +40,6 @@ def run(mode):
4440
print("write block %d successfully" % block_number)
4541
except nfc.PN532Error as e:
4642
print(e.errmsg)
47-
finally:
48-
GPIO.cleanup()
4943

5044

5145
def main():

src/pn532/example/uid.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
After initialization, try waving various 13.56MHz RFID cards over it!
99
"""
1010

11-
import RPi.GPIO as GPIO # noqa: N814
12-
1311
from pn532.example import parse_mode
1412

1513

1614
def run(mode):
1715
"""Run."""
18-
try:
19-
pn532 = mode()
20-
16+
with mode() as pn532:
2117
ic, ver, rev, support = pn532.get_firmware_version()
2218
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
2319

@@ -33,10 +29,6 @@ def run(mode):
3329
if uid is None:
3430
continue
3531
print("Found card with UID:", [hex(i) for i in uid])
36-
except Exception as e:
37-
print(e)
38-
finally:
39-
GPIO.cleanup()
4032

4133

4234
def main():

0 commit comments

Comments
 (0)