Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--stdnum/pl/regon.py95
-rw-r--r--tests/test_pl_regon.doctest104
2 files changed, 199 insertions, 0 deletions
diff --git a/stdnum/pl/regon.py b/stdnum/pl/regon.py
new file mode 100644
index 0000000..5099b55
--- /dev/null
+++ b/stdnum/pl/regon.py
@@ -0,0 +1,95 @@
+# pesel.py - functions for handling REGON numbers
+# coding: utf-8
+#
+# Copyright (C) 2015 Dariusz Choruzy
+# Copyright (C) 2015 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
+
+"""REGON (Rejestr Gospodarki Narodowej, Polish register of economic units).
+
+The REGON (Rejestr Gospodarki Narodowej) is a statistical identification
+number for businesses. National entities are assigned a 9-digit number, while
+local units append 5 digits to form a 14-digit number.
+
+More information can be found at:
+ http://bip.stat.gov.pl/en/regon/
+ http://www.stat.gov.pl/bip/regon_ENG_HTML.htm
+ https://wyszukiwarkaregon.stat.gov.pl/appBIR/index.aspx
+
+>>> validate('192598184')
+'192598184'
+>>> validate('123456785')
+'123456785'
+>>> validate('192598183')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> validate('12345678512347')
+'12345678512347'
+>>> validate('12345678612342') # first check digit invalid
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> validate('12345678512348') # last check digit invalid
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+def compact(number):
+ """Convert the number to the minimal representation. This strips the
+ number of any valid separators and removes surrounding whitespace."""
+ return clean(number, ' -').upper().strip()
+
+
+def calc_check_digit(number):
+ """Calculate the check digit for organisations. The number passed
+ should not have the check digit included."""
+ if len(number) == 8:
+ weights = (8, 9, 2, 3, 4, 5, 6, 7)
+ else:
+ weights = (2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8)
+ check = sum(weights[i] * int(n) for i, n in enumerate(number))
+ return str(check % 11 % 10)
+
+
+def validate(number):
+ """Checks to see if the number provided is a valid REGON number. This
+ checks the length, formatting and check digit."""
+ number = compact(number)
+ if not number.isdigit():
+ raise InvalidFormat()
+ if len(number) not in (9, 14):
+ raise InvalidLength()
+ if number[-1] != calc_check_digit(number[:-1]):
+ raise InvalidChecksum()
+ if len(number) == 14 and number[8] != calc_check_digit(number[:8]):
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid REGON number. This
+ checks the length, formatting and check digit."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
diff --git a/tests/test_pl_regon.doctest b/tests/test_pl_regon.doctest
new file mode 100644
index 0000000..ecd4f69
--- /dev/null
+++ b/tests/test_pl_regon.doctest
@@ -0,0 +1,104 @@
+test_pl_regon.doctest - more detailed doctests for the stdnum.pl.regon module
+
+Copyright (C) 2015 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.pl.regon module. It
+tries to validate a number of numbers that have been found online.
+
+>>> from stdnum.pl import regon
+>>> from stdnum.exceptions import *
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 000144992
+... 000262289
+... 000569734
+... 001123710
+... 001130740
+... 001130762
+... 001130785
+... 004611261
+... 010925233
+... 011370116
+... 015615046
+... 015831301
+... 017282442
+... 021425170
+... 021493503
+... 061245300
+... 080121017
+... 101286521
+... 101624716
+... 101702037
+... 120444729
+... 120812966
+... 122903064
+... 122989806
+... 140006787
+... 140124720
+... 140567178
+... 140906290
+... 141215688
+... 147244188
+... 160178314
+... 180405830
+... 180562050
+... 190248215
+... 190384415
+... 191305139
+... 192976380
+... 200197338
+... 220218697
+... 221997249
+... 241675487
+... 271747631
+... 276273209
+... 280002520
+... 292449593
+... 300613124
+... 302406613
+... 357126121
+... 360935507
+... 362392991
+... 362501144
+... 362509447
+... 362509460
+... 362509476
+... 362523660
+... 370418951
+... 370452988
+... 432740426
+... 570007868
+... 570135170
+... 570271370
+... 570860528
+... 572134252
+... 630679076
+... 632154969
+... 634389910
+... 670141210
+... 810033277
+... 812663628
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not regon.is_valid(x)]
+[]