1# lei.py - functions for handling Legal Entity Identifiers (LEIs) 2# coding: utf-8 3# 4# Copyright (C) 2017 Arthur de Jong 5# 6# This library is free software; you can redistribute it and/or 7# modify it under the terms of the GNU Lesser General Public 8# License as published by the Free Software Foundation; either 9# version 2.1 of the License, or (at your option) any later version. 10# 11# This library is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14# Lesser General Public License for more details. 15# 16# You should have received a copy of the GNU Lesser General Public 17# License along with this library; if not, write to the Free Software 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19# 02110-1301 USA 20 21"""LEI (Legal Entity Identifier). 22 23The Legal Entity Identifier (LEI) is used to identify legal entities for use 24in financial transactions. A LEI is a 20-character alphanumeric string that 25consists of a 4-character issuing LOU (Local Operating Unit), 2 digits that 26are often 0, 13 digits to identify the organisation and 2 check digits. 27 28More information: 29 30* https://en.wikipedia.org/wiki/Legal_Entity_Identifier 31* http://www.lei-lookup.com/ 32* https://www.gleif.org/ 33* http://openleis.com/ 34 35>>> validate('213800KUD8LAJWSQ9D15') 36'213800KUD8LAJWSQ9D15' 37>>> validate('213800KUD8LXJWSQ9D15') 38Traceback (most recent call last): 39 ... 40InvalidChecksum: ... 41""" 42 43from stdnum.exceptions import * 44from stdnum.iso7064 import mod_97_10 45from stdnum.util import clean 46 47 48def compact(number): 49 """Convert the number to the minimal representation. This strips the 50 number of any valid separators and removes surrounding white space.""" 51 return clean(number, ' -').strip().upper() 52 53 54def validate(number): 55 """Check if the number is valid. This checks the length, format and check 56 digits.""" 57 number = compact(number) 58 mod_97_10.validate(number) 59 return number 60 61 62def is_valid(number): 63 """Check if the number is valid.""" 64 try: 65 return bool(validate(number)) 66 except ValidationError: 67 return False 68