Skip to content

Commit e4c67c9

Browse files
author
brentru
committed
done linting
1 parent bb740ae commit e4c67c9

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

adafruit_binascii.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,43 @@
4848
__version__ = "0.0.0-auto.0"
4949
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_binascii.git"
5050

51-
# pylint: disable=bad-whitespace
51+
5252
TABLE_A2B_B64 = [
53-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
56-
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
53+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
56+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
5757
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
58-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1, -1, -1, -1, -1,
59-
-1, 26, 27, 28, 29, 30,31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1, -1, -1, -1, -1,
61-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
59+
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
61+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
6969
]
7070

71+
TABLE_B2A_B64 = (
72+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
73+
74+
class Error(Exception):
75+
"""Exception raised on errors. These are usually programming errors."""
76+
# pylint: disable=unnecessary-pass
77+
pass
7178

7279
if not "unhexlify" in globals():
7380
# pylint: disable=function-redefined
7481
def unhexlify(hexstr):
7582
"""Return the binary data represented by hexstr.
7683
:param str hexstr: Hexadecimal string.
84+
7785
"""
7886
if len(hexstr) % 2 != 0:
79-
raise ValueError("Odd-length string")
87+
raise Error("Odd-length string")
8088

8189
return bytes([int(hexstr[i : i + 2], 16) for i in range(0, len(hexstr), 2)])
8290

@@ -91,6 +99,7 @@ def hexlify(data):
9199
as long as the length of data.
92100
93101
:param bytes data: Binary data, as bytes.
102+
94103
"""
95104
if not data:
96105
raise TypeError("Data provided is zero-length")
@@ -108,7 +117,11 @@ def _transform(n):
108117
assert len(TABLE_A2B_B64) == 256
109118

110119
def a2b_base64(b64_data):
111-
"Decode a line of base64 data."
120+
"""Convert a block of base64 data back to binary and return the binary data.
121+
122+
:param str b64_data: Base64 data.
123+
124+
"""
112125
res = []
113126
quad_pos = 0
114127
leftchar = 0
@@ -144,14 +157,12 @@ def a2b_base64(b64_data):
144157

145158
return b''.join(res)
146159

147-
# ____________________________________________________________
148-
149-
TABLE_B2A_B64 = (
150-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
151-
152160
def b2a_base64(bin_data):
153-
"Base64-code line of data."
161+
"""Convert binary data to a line of ASCII characters in base64 coding.
162+
163+
:param str bin_data: Binary data string, as bytes
154164
165+
"""
155166
newlength = (len(bin_data) + 2) // 3
156167
newlength = newlength * 4 + 1
157168
res = []

examples/binascii_simpletest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from adafruit_binascii import hexlify, unhexlify, a2b_base64, b2a_base64
2+
3+
# Binary data.
4+
data = b"CircuitPython is Awesome!"
5+
6+
# Get the hexadecimal representation of the binary data
7+
hex_data = hexlify(data)
8+
# Verify data
9+
assert hex_data == b"43697263756974507974686f6e20697320417765736f6d6521", "hexlified data does not match expected data."
10+
11+
# Get the binary data represented by hex_data
12+
bin_data = unhexlify(hex_data)
13+
# Verify data
14+
assert bin_data == data, "unhexlified binary data does not match original binary data."
15+
16+
# Convert binary data to a line of ASCII characters in base64 coding.
17+
assert b2a_base64(b"Blinka") == b"Qmxpbmth\n", "Expected base64 coding does not match."
18+
19+
# Convert a block of base64 data back to binary data.
20+
assert a2b_base64(b"Qmxpbmth\n") == b"Blinka", "Expected binary data does not match."

0 commit comments

Comments
 (0)