Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2017-04-10 22:21:45 +0200
committerArthur de Jong <arthur@arthurdejong.org>2017-04-10 22:51:55 +0200
commit53982477586198b2e22ec16d30a8d4e371191d70 (patch)
treeecce71c34343c1f583f932139d1a7ffe1b25c52d
parente844b5235a1a0599cabe6a5f1b27228b3036db05 (diff)
Add Legal Entity Identifier
-rw-r--r--stdnum/lei.py68
-rw-r--r--tests/test_lei.doctest134
2 files changed, 202 insertions, 0 deletions
diff --git a/stdnum/lei.py b/stdnum/lei.py
new file mode 100644
index 0000000..f87bbee
--- /dev/null
+++ b/stdnum/lei.py
@@ -0,0 +1,68 @@
+# lei.py - functions for handling Legal Entity Identifiers (LEIs)
+# coding: utf-8
+#
+# Copyright (C) 2017 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
+
+"""LEI (Legal Entity Identifier).
+
+The Legal Entity Identifier (LEI) is used to identify legal entities for use
+in financial transactions. A LEI is a 20-character alphanumeric string that
+consists of a 4-character issuing LOU (Local Operating Unit), 2 digits that
+are often 0, 13 digits to identify the organisation and 2 check digits.
+
+More information:
+
+* https://en.wikipedia.org/wiki/Legal_Entity_Identifier
+* http://www.lei-lookup.com/
+* https://www.gleif.org/
+* http://openleis.com/
+
+>>> validate('213800KUD8LAJWSQ9D15')
+'213800KUD8LAJWSQ9D15'
+>>> validate('213800KUD8LXJWSQ9D15')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+"""
+
+from stdnum.exceptions import *
+from stdnum.iso7064 import mod_97_10
+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 white space."""
+ return clean(number, ' -').strip().upper()
+
+
+def validate(number):
+ """Checks to see if the number provided is valid. This checks the length,
+ format and check digits."""
+ number = compact(number)
+ mod_97_10.validate(number)
+ return number
+
+
+def is_valid(number):
+ """Checks to see if the number provided is valid. This checks the length,
+ format and check digits."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
diff --git a/tests/test_lei.doctest b/tests/test_lei.doctest
new file mode 100644
index 0000000..5c2900d
--- /dev/null
+++ b/tests/test_lei.doctest
@@ -0,0 +1,134 @@
+test_lei.doctest - more detailed doctests for the stdnum.lei module
+
+Copyright (C) 2017 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.lei module.
+
+>>> from stdnum import lei
+>>> from stdnum.exceptions import *
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 0YPKKE5F0QW6RC51HE09
+... 1YTX3EVB2EG3PFFYOI09
+... 2138001CY61HDFJ5ZA27
+... 2138001KT6BLFA2SBA38
+... 213800699Y1P5GMARI40
+... 213800DIH9U9264UW504
+... 213800F25B5OHORTSI52
+... 213800KLHG79RR4RVR16
+... 213800KNMQ53LTJQBV92
+... 213800KUD8LAJWSQ9D15
+... 213800Q3CZMOFF9OE643
+... 213800REANX5GCW7AD17
+... 213800THICTTOSR1N623
+... 213800WWDZ5X6SRDF737
+... 213800WWNILTD76WIX72
+... 2594005N6NMJM5WSGE40
+... 2YXI7YIWYFWZG3QHF204
+... 315700EBSH5FQO0A2E52
+... 315700UUG8ME8T3NNN23
+... 337KMNHEWWWR6B7Q7W10
+... 391200J69OE1D0B3ZQ28
+... 3YO0DHNPQLJN0PYQXZ94
+... 5299007DT4PDT06SZM22
+... 5299008A9PK6IQEWE268
+... 529900TODVLNUTNSYF94
+... 529900UJX7YWK7YK5Z47
+... 529900WWBCVXKSBVKS40
+... 529900YGP0ANNLITEF34
+... 529900Z5WSAAJ3OTQU15
+... 549300001TP7X0VE9866
+... 54930000YBLXA2M5DV57
+... 54930007KCQLYRQLSU30
+... 5493000MYJ7H0E3KKG91
+... 54930017EFV0P0QWP015
+... 54930018TDVHWGLNZ954
+... 5493001BD9HCE74UH411
+... 5493004NJH3JGW7MVS62
+... 5493004QRTOY0WERIJ41
+... 54930066SVUPVIHE5U21
+... 5493006O7NV2IXKS4288
+... 5493007715JF1TY3M614
+... 5493007HHN4G8PA5MS11
+... 5493007PWOC8DW5KJM17
+... 5493008EWHY43YYP8R93
+... 5493008VZPPOQ8Y63J10
+... 549300EF33KFNMISKX67
+... 549300EUCOJ6XD50YM58
+... 549300FR4NAW0G8UFI80
+... 549300FRSHK45MQIBL14
+... 549300GF102RMNYVKB19
+... 549300H45ZBEKCVW0U40
+... 549300IDQBTGQCXJ5239
+... 549300IWFWPPXW87ZE72
+... 549300J8EHPRYOKF3869
+... 549300JMX497I8GHDT24
+... 549300LHQ6XOM0JUW406
+... 549300M3SJFSFVXG6X69
+... 549300NV50SCQMF8OC75
+... 549300O2F2PCD1D7PZ95
+... 549300O7ZFXE3YT1GH43
+... 549300OIVSR86BGJGT75
+... 549300OM88E30DLQTD78
+... 549300PATTPXQ660N070
+... 549300PFL3HXXEEOHS50
+... 549300SILMFILZ8Y4427
+... 549300SOEQOO5LPOD659
+... 549300TZLPZOAIIMJF22
+... 549300UKFZ3BD7TNKI26
+... 549300V6KHTZPJ4YVC33
+... 549300V6LZG40THFO450
+... 549300V885B988S1LJ49
+... 549300WTO22HTNIA0Z19
+... 549300X8N7DWRL7VBF44
+... 549300XBRMGPGXSSB667
+... 549300YBGK6ESSY6G874
+... 549300YPYUJBJGNGQO81
+... 5967007LIEEXZX850W58
+... 6354004RBF2LINM9VH78
+... 635400ID2ZOCFWX3LH70
+... 635400TSTDEIQBVFXR91
+... 7245005BAK6R2JQ1LU94
+... 7245006M2FC6DPMC6427
+... 724500PVWFYKZIJQAN07
+... 815600065E4EA9D08446
+... 8156004572781E3F9023
+... 815600E6B00CF4DBB722
+... 815600FC0039E657C985
+... 815600FF404253C67598
+... 959800Q824KQDPCZPV58
+... 96950091S6OFL0N15G96
+... 969500BL7YE3PXKDYT35
+... 969500YLLNN3TK3JSF66
+... 969500ZAUC3Z50DNZV77
+... FIR47I6FEYKYNJBYW622
+... GU00WJXK6DH4VHHFXQ16
+... NMMFE09VSMAF2TU16C07
+... RCJ8N5WH4YK7SVJ4BO12
+... TGXITECVNFSIBV316765
+... WT03B8BB1IX8WI9ZGV02
+... XNIO7KHWR2WD0BP1F484
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not lei.is_valid(x)]
+[]