Skip to content

Commit

Permalink
Replace try...finally with contextmanagers
Browse files Browse the repository at this point in the history
  • Loading branch information
jlubken committed Oct 17, 2020
1 parent 02d4fd3 commit e45efc6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 64 deletions.
33 changes: 24 additions & 9 deletions src/pn532/example/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"""Example."""

from argparse import ArgumentParser
from contextlib import contextmanager

from RPi.GPIO import GPIO # noqa: N814

from pn532 import (
PN532_SPI,
Expand All @@ -10,19 +13,31 @@
)


def spi():
"""Return default spi device."""
return PN532_SPI(debug=False, reset=20, cs=4)
@contextmanager
def spi(debug=False, reset=20, cs=4):
"""Yield a spi device."""
try:
yield PN532_SPI(debug=debug, reset=reset, cs=cs)
finally:
GPIO.cleanup()


def i2c():
"""Return default i2c device."""
return PN532_I2C(debug=False, reset=20, req=16)
@contextmanager
def i2c(debug=False, reset=20, req=16):
"""Yield a i2c device."""
try:
yield PN532_I2C(debug=debug, reset=reset, req=req)
finally:
GPIO.cleanup()


def uart():
"""Return default uart device."""
return PN532_UART(debug=False, reset=20)
@contextmanager
def uart(debug=False, reset=20):
"""Yield a uart device."""
try:
yield PN532_UART(debug=debug, reset=reset)
finally:
GPIO.cleanup()


def mode(value):
Expand Down
8 changes: 1 addition & 7 deletions src/pn532/example/gpio/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
This example shows connecting to the PN532 and reading the GPIOs.
"""

import RPi.GPIO as GPIO # noqa: N814

from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

Expand All @@ -29,8 +25,6 @@ def run(mode):
print("i0:", pn532.read_gpio("I0"))
print("i1:", pn532.read_gpio("I1"))
print("P34:", pn532.read_gpio("P34"))
finally:
GPIO.cleanup()


def main():
Expand Down
8 changes: 1 addition & 7 deletions src/pn532/example/gpio/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
This example shows connecting to the PN532 and writing the GPIOs.
"""

import RPi.GPIO as GPIO # noqa: N814

from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

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


def main():
Expand Down
8 changes: 1 addition & 7 deletions src/pn532/example/mifare/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
type RFID tag
"""

import RPi.GPIO as GPIO # noqa: N814

import pn532.pn532 as nfc

from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

Expand Down Expand Up @@ -57,8 +53,6 @@ def run(mode):
except nfc.PN532Error as e:
print(e.errmsg)
break
finally:
GPIO.cleanup()


def main():
Expand Down
10 changes: 3 additions & 7 deletions src/pn532/example/mifare/rw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
"""
Read/write mifare.
This example shows connecting to the PN532 and writing an M1
type RFID tag
Expand All @@ -13,17 +15,13 @@
2. Block 0 is unwritable.
"""

import RPi.GPIO as GPIO # noqa: N814

import pn532.pn532 as nfc
from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

Expand Down Expand Up @@ -86,8 +84,6 @@ def run(mode):
print("write block %d successfully" % block_number)
except nfc.PN532Error as e:
print(e.errmsg)
finally:
GPIO.cleanup()


def main():
Expand Down
12 changes: 2 additions & 10 deletions src/pn532/example/ntag2/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
type RFID tag
"""

import RPi.GPIO as GPIO # noqa: N814

import pn532.pn532 as nfc
from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print(
" Found PN532 with firmware version: {0}.{1}".format(ver, rev)
)
print(f"Found PN532 with firmware version: {ver}.{rev}")

# Configure PN532 to communicate with NTAG215 cards
pn532.SAM_configuration()
Expand Down Expand Up @@ -48,8 +42,6 @@ def run(mode):
except nfc.PN532Error as e:
print(e.errmsg)
break
finally:
GPIO.cleanup()


def main():
Expand Down
10 changes: 2 additions & 8 deletions src/pn532/example/ntag2/rw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
type RFID tag
"""

import RPi.GPIO as GPIO # noqa: N814

import pn532.pn532 as nfc

from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
print("Found PN532 with firmware version: {ver}.{rev}")

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


def main():
Expand Down
10 changes: 1 addition & 9 deletions src/pn532/example/uid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
After initialization, try waving various 13.56MHz RFID cards over it!
"""

import RPi.GPIO as GPIO # noqa: N814

from pn532.example import parse_mode


def run(mode):
"""Run."""
try:
pn532 = mode()

with mode() as pn532:
ic, ver, rev, support = pn532.get_firmware_version()
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

Expand All @@ -33,10 +29,6 @@ def run(mode):
if uid is None:
continue
print("Found card with UID:", [hex(i) for i in uid])
except Exception as e:
print(e)
finally:
GPIO.cleanup()


def main():
Expand Down

0 comments on commit e45efc6

Please sign in to comment.