8000 Small code cleanups · Daverball/python-stdnum@6b9bbe2 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 6b9bbe2

Browse files
Daverballarthurdejong
authored andcommitted
Small code cleanups
1 parent 3542c06 commit 6b9bbe2

File tree

9 files changed

+22
-19
lines changed

9 files changed

+22
-19
lines changed

stdnum/be/iban.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ def info(number):
7474

7575
def to_bic(number):
7676
"""Return the BIC for the bank that this number refers to."""
77-
bic = info(number).get('bic')
78-
if bic:
79-
return str(bic)
77+
return info(number).get('bic')
8078

8179

8280
def validate(number):

stdnum/be/nn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def get_birth_month(number):
182182
def get_birth_date(number):
183183
"""Return the date of birth."""
184184
year, month, day = _get_birth_date_parts(compact(number))
185-
if None not in (year, month, day):
185+
if year and month and day:
186186
return datetime.date(year, month, day)
187187

188188

stdnum/cz/bankaccount.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ def info(number):
9797

9898
def to_bic(number):
9999
"""Return the BIC for the bank that this number refers to."""
100-
bic = info(number).get('bic')
101-
if bic:
102-
return str(bic)
100+
return info(number).get('bic')
103101

104102

105103
def _calc_checksum(number):

stdnum/es/cups.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def validate(number):
9494
raise InvalidComponent()
9595
if not isdigits(number[2:18]):
9696
raise InvalidFormat()
97-
if number[20:]:
98-
pnumber, ptype = number[20:]
97+
if len(number) == 22:
98+
pnumber, ptype = number[20], number[21]
9999
if not isdigits(pnumber):
100100
raise InvalidFormat()
101101
if ptype not in 'FPRCXYZ':

stdnum/gs1_128.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def info(number, separator=''):
197197
number = number[len(ai):]
198198
# figure out the value part
199199
value = number[:_max_length(info['format'], info['type'])]
200-
if separator and info.get('fnc1', False):
200+
if separator and info.get('fnc1'):
201201
idx = number.find(separator)
202202
if idx > 0:
203203
value = number[:idx]
@@ -243,7 +243,7 @@ def encode(data, separator='', parentheses=False):
243243
mod.validate(value)
244244
value = _encode_value(info['format'], info['type'], value)
245245
# store variable-sized values separate from fixed-size values
246-
if info.get('fnc1', False):
246+
if info.get('fnc1'):
247247
variable_values.append((ai_fmt % ai, info['format'], info['type'], value))
248248
else:
249249
fixed_values.append(ai_fmt % ai + value)

stdnum/pk/cnic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_gender(number):
7878

7979

8080
def get_province(number):
81-
"""Get the person's birth gender ('M' or 'F')."""
81+
"""Get the person's province."""
8282
number = compact(number)
8383
return PROVINCES.get(number[0])
8484

stdnum/vatin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
The number VAT identification number (VATIN) is an identifier used in many
2424
countries. It starts with an ISO 3166-1 alpha-2 (2 letters) country code
2525
(except for Greece, which uses EL, instead of GR) and is followed by the
26-
country-specific the identifier.
26+
country-specific identifier.
2727
2828
This module supports all VAT numbers that are supported in python-stdnum.
2929
@@ -65,9 +65,10 @@ def _get_cc_module(cc):
6565
raise InvalidFormat()
6666
if cc not in _country_modules:
6767
_country_modules[cc] = get_cc_module(cc, 'vat')
68-
if not _country_modules[cc]:
68+
module = _country_modules[cc]
69+
if not module:
6970
raise InvalidComponent() # unknown/unsupported country code
70-
return _country_modules[cc]
71+
return module
7172

7273

7374
def compact(number):

stdnum/verhoeff.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@
7777
def checksum(number):
7878
"""Calculate the Verhoeff checksum over the provided number. The checksum
7979
is returned as an int. Valid numbers should have a checksum of 0."""
80-
# transform number list
81-
number = tuple(int(n) for n in reversed(str(number)))
82-
# calculate checksum
8380
check = 0
84-
for i, n in enumerate(number):
81+
for i, n in enumerate(int(n) for n in reversed(str(number))):
8582
check = _multiplication_table[check][_permutation_table[i % 8][n]]
8683
return check
8784

tests/test_be_iban.doctest

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ Traceback (most recent call last):
3737
InvalidChecksum: ...
3838

3939

40+
Some bank account numbers don't have a BIC.
41+
(this is a constructed bank account number, no actual one was found in the wild)
42+
43+
>>> iban.validate('BE45102000000089')
44+
'BE45102000000089'
45+
>>> iban.to_bic('BE45102000000089') is None
46+
True
47+
48+
4049
These should all be valid numbers combined with the appropriate BIC code for
4150
the IBAN.
4251

0 commit comments

Comments
 (0)
0