def generate(cls, country_code, bank_code, account_code, branch_code=""): """Generate an IBAN from it's components. If the bank-code and/or account-number have less digits than required by their country specific representation, the respective component is padded with zeros. Examples: To generate an IBAN do the following:: >>> bank_code = '37040044' >>> account_code = '532013000' >>> iban = IBAN.generate('DE', bank_code, account_code) >>> iban.formatted 'DE89 3704 0044 0532 0130 00' Args: country_code (str): The ISO 3166 alpha-2 country code. bank_code (str): The country specific bank-code. account_code (str): The customer specific account-code. .. versionchanged:: 2020.08.3 Added the `branch_code` parameter to allow the branch code (or sort code) to be specified independently. """ spec = _get_iban_spec(country_code) bank_code_length = code_length(spec, "bank_code") branch_code_length = code_length(spec, "branch_code") account_code_length = code_length(spec, "account_code") if len(bank_code) == bank_code_length + branch_code_length: bank_code, branch_code = bank_code[:bank_code_length], bank_code[ bank_code_length:] if len(bank_code) > bank_code_length: raise exceptions.InvalidBankCode( f"Bank code exceeds maximum size {bank_code_length}") if len(branch_code) > branch_code_length: raise exceptions.InvalidBranchCode( f"Branch code exceeds maximum size {branch_code_length}") if len(account_code) > account_code_length: raise exceptions.InvalidAccountCode( f"Account code exceeds maximum size {account_code_length}") bban = "0" * spec["bban_length"] positions = spec["positions"] components = { "bank_code": bank_code, "branch_code": branch_code, "account_code": account_code, } for key, value in components.items(): end = positions[key][1] start = end - len(value) bban = bban[:start] + value + bban[end:] bban = add_bban_checksum(country_code, bban) return cls(country_code + "??" + bban)
def from_bank_code(cls, country_code: str, bank_code: str) -> BIC: """Create a new BIC object from country-code and domestic bank-code. Examples: >>> bic = BIC.from_bank_code('DE', '20070000') >>> bic.country_code 'DE' >>> bic.bank_code 'DEUT' >>> bic.location_code 'HH' >>> BIC.from_bank_code('DE', '01010101') Traceback (most recent call last): ... InvalidBankCode: Unknown bank code '01010101' for country 'DE' Args: country_code (str): ISO 3166 alpha2 country-code. bank_code (str): Country specific bank-code. Returns: BIC: a BIC object generated from the given country code and bank code. Raises: InvalidBankCode: If the given bank code wasn't found in the registry Note: This currently only works for selected countries. Amongst them * Austria * Belgium * Croatia * Czech Republic * Finland * France * Germany * Great Britan * Latvia * Lithuania * Netherlands * Poland * Slovenia * Spain * Switzerland """ try: spec = registry.get("bank_code") assert isinstance(spec, dict) return cls(spec[(country_code, bank_code)]["bic"]) except KeyError: raise exceptions.InvalidBankCode( f"Unknown bank code {bank_code!r} for country {country_code!r}" )