1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
test_de_stnr.doctest - more detailed doctests for the stdnum.de.stnr module
Copyright (C) 2017 Holvi Payment Services
Copyright (C) 2018 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
This file contains more detailed doctests for the stdnum.de.stnr module. It
tries to validate a number of numbers that have been found online.
>>> from stdnum.de import stnr
Some simple tests.
>>> stnr.validate('1123456789')
'1123456789'
>>> stnr.validate('1123456789', 'Berlin')
'1123456789'
>>> stnr.validate('12234567890', 'Berlin')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> stnr.validate('1123456789', 'Unknown region')
Traceback (most recent call last):
...
InvalidComponent: ...
>>> stnr.validate('1234567890') # 10-digit number
'1234567890'
>>> stnr.validate('12345678901') # 11-digit number
'12345678901'
>>> stnr.validate('1123045678901') # 13-digit number
'1123045678901'
>>> stnr.validate('123456789') # short number
Traceback (most recent call last):
...
InvalidLength: ...
The module should handle various encodings of region names properly.
>>> stnr.validate('9381508152', u'Baden-W\xfcrttemberg') # Python unicode
'9381508152'
>>> stnr.validate('9381508152', 'Baden-W\xc3\xbcrttemberg') # UTF-8
'9381508152'
>>> stnr.validate('9381508152', 'Baden-W\xfcrttemberg') # ISO-8859-15
'9381508152'
>>> stnr.validate('9381508152', 'Baden Wurttemberg') # ASCII with space
'9381508152'
Given a number we are able to find a region.
>>> stnr.guess_regions('1123045678901') # 13-digit number
['Berlin']
>>> stnr.guess_regions('98765432101') # 11-digit number
['Bayern', 'Nordrhein-Westfalen']
>>> stnr.guess_regions('123') # invalid number
[]
We can convert the 13-digit country number to a regional number without
issues. We can also convert it back if we know the region.
>>> stnr.guess_regions('2475081508152')
['Bremen']
>>> stnr.to_regional_number('2475081508152')
'7581508152'
>>> stnr.validate('7581508152', 'Bremen')
'7581508152'
>>> stnr.to_regional_number('123')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> stnr.to_country_number('7581508152', 'Bremen')
'2475081508152'
>>> stnr.to_country_number('7581508152') # not unique, need region
Traceback (most recent call last):
...
InvalidComponent: ...
>>> stnr.to_country_number('123')
Traceback (most recent call last):
...
InvalidFormat: ...
We can also format numbers by separating the groups with slashes. This is
most often seen for regional numbers and the 13-digit numbers don't get the
slashes.
>>> stnr.format('18181508155', 'Bayern')
'181/815/08155'
>>> stnr.format('18181508155', 'Nordrhein-Westfalen')
'181/8150/8155'
>>> stnr.format('2181508150')
'21/815/08150'
>>> stnr.format('156 / 141 / 14808', 'Thuringen')
'156/141/14808'
>>> stnr.format('2893081508152') # 13-digit number
'2893081508152'
>>> stnr.format('123') # unknown format
'123'
These have been found online and should all be valid numbers.
>>> numbers = '''
...
... 010/815/08182
... 013 815 08153
... 02/815/08156
... 04 522 658 002
... 042/213/02423
... 048/815/08155
... 079/815/08151
... 101/5761/1744
... 101/815/08154
... 1010081508182
... 1121081508150
... 116/5701/1448
... 123/456/7890
... 133/5909/3295
... 133/8150/8159
... 14044/00050
... 143/317/22090
... 147/276/80579
... 151/815/08156
... 156 / 141 / 14808
... 162/107/03482
... 181/815/08155
... 1929008636
... 201/123/12340
... 201/5902/3626
... 201/5906/3686
... 202/ 106/ 08312
... 203/100/04333
... 20418290688
... 208/140/04075
... 21/815/08150
... 212/5730/0455
... 2129081508158
... 22/815/08154
... 220/5769/0078
... 2202081508156
... 2324081508151
... 24/815/08151
... 2475081508152
... 249/115/90057
... 249/133/90020
... 26 242 02421
... 2613081508153
... 27 173 00028
... 27/673/50365
... 2722081508154
... 2893081508152
... 29/815/08158
... 3048081508155
... 307/5904/0270
... 3101081508154
... 312/5120/1726
... 313/5753/1315
... 3201012312340
... 332/5751/2 653
... 332/5776/0076
... 339/5822/0944
... 342/5938/0307
... 4079081508151
... 4151081508156
... 5133081508159
... 75 815 08152
... 76 001/12 885
... 9181081508155
... 93815/08152
... 99015/28445
... 99019132055
...
... '''
>>> [x for x in numbers.splitlines() if x and not stnr.is_valid(x)]
[]
|