diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2022-08-15 13:59:26 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2022-08-15 14:04:06 +0200 |
commit | eae1dd298e56b565af394d6fc85ead89fbbf8565 (patch) | |
tree | 3d65386c9b7b2fde9b555eea1ad6ddb8fef013ca /stdnum | |
parent | c5595c7e80277bbf2102f293c1f4fbcec07f7101 (diff) |
Use str.zfill() for padding leading zeros
Diffstat (limited to 'stdnum')
-rw-r--r-- | stdnum/ch/esr.py | 3 | ||||
-rw-r--r-- | stdnum/il/idnr.py | 4 | ||||
-rw-r--r-- | stdnum/isin.py | 3 | ||||
-rw-r--r-- | stdnum/nl/bsn.py | 4 | ||||
-rw-r--r-- | stdnum/no/kontonr.py | 3 | ||||
-rw-r--r-- | stdnum/nz/ird.py | 2 | ||||
-rw-r--r-- | stdnum/ro/cui.py | 2 |
7 files changed, 7 insertions, 14 deletions
diff --git a/stdnum/ch/esr.py b/stdnum/ch/esr.py index 862df78..8d62380 100644 --- a/stdnum/ch/esr.py +++ b/stdnum/ch/esr.py @@ -92,7 +92,6 @@ def is_valid(number): def format(number): """Reformat the number to the standard presentation format.""" - number = 27 * '0' + compact(number) - number = number[-27:] + number = compact(number).zfill(27) return number[:2] + ' ' + ' '.join( number[i:i + 5] for i in range(2, len(number), 5)) diff --git a/stdnum/il/idnr.py b/stdnum/il/idnr.py index 544d6df..545f5fc 100644 --- a/stdnum/il/idnr.py +++ b/stdnum/il/idnr.py @@ -51,9 +51,7 @@ from stdnum.util import clean, isdigits def compact(number): """Convert the number to the minimal representation. This strips the number of any valid separators and removes surrounding whitespace.""" - number = clean(number, ' -').strip() - # pad with leading zeroes - return (9 - len(number)) * '0' + number + return clean(number, ' -').strip().zfill(9) def validate(number): diff --git a/stdnum/isin.py b/stdnum/isin.py index e1bbfca..1d48f88 100644 --- a/stdnum/isin.py +++ b/stdnum/isin.py @@ -129,6 +129,5 @@ def is_valid(number): def from_natid(country_code, number): """Generate an ISIN from a national security identifier.""" - number = compact(number) - number = country_code.upper() + (9 - len(number)) * '0' + number + number = country_code.upper() + compact(number).zfill(9) return number + calc_check_digit(number) diff --git a/stdnum/nl/bsn.py b/stdnum/nl/bsn.py index b869100..c3b3583 100644 --- a/stdnum/nl/bsn.py +++ b/stdnum/nl/bsn.py @@ -51,9 +51,7 @@ from stdnum.util import clean, isdigits def compact(number): """Convert the number to the minimal representation. This strips the number of any valid separators and removes surrounding whitespace.""" - number = clean(number, ' -.').strip() - # pad with leading zeroes - return (9 - len(number)) * '0' + number + return clean(number, ' -.').strip().zfill(9) def checksum(number): diff --git a/stdnum/no/kontonr.py b/stdnum/no/kontonr.py index 1a466e0..addc767 100644 --- a/stdnum/no/kontonr.py +++ b/stdnum/no/kontonr.py @@ -96,8 +96,7 @@ def to_iban(number): def format(number): """Reformat the number to the standard presentation format.""" - number = compact(number) - number = (11 - len(number)) * '0' + number + number = compact(number).zfill(11) return '.'.join([ number[:4], number[4:6], diff --git a/stdnum/nz/ird.py b/stdnum/nz/ird.py index 0e9307a..6bd313b 100644 --- a/stdnum/nz/ird.py +++ b/stdnum/nz/ird.py @@ -66,7 +66,7 @@ def calc_check_digit(number): primary_weights = (3, 2, 7, 6, 5, 4, 3, 2) secondary_weights = (7, 4, 3, 2, 5, 2, 7, 6) # pad with leading zeros - number = (8 - len(number)) * '0' + number + number = number.zfill(8) s = -sum(w * int(n) for w, n in zip(primary_weights, number)) % 11 if s != 10: return str(s) diff --git a/stdnum/ro/cui.py b/stdnum/ro/cui.py index 8e88840..fbfad98 100644 --- a/stdnum/ro/cui.py +++ b/stdnum/ro/cui.py @@ -59,7 +59,7 @@ def compact(number): def calc_check_digit(number): """Calculate the check digit.""" weights = (7, 5, 3, 2, 1, 7, 5, 3, 2) - number = (9 - len(number)) * '0' + number + number = number.zfill(9) check = 10 * sum(w * int(n) for w, n in zip(weights, number)) return str(check % 11 % 10) |