Last active
September 6, 2019 20:17
-
-
Save birkin/0bf7c1c3352b0d7416f6b655c0161894 to your computer and use it in GitHub Desktop.
sierra check-digit logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Usage: $ python3 /path/to/sierra_check_digit.py 15990359 | |
| or: $ python3 /path/to/sierra_check_digit.py 15990359 DEBUG | |
| ''' | |
| import logging, sys | |
| log = logging.getLogger(__name__) | |
| def build_check_digit( raw_item_id ): | |
| """ Calculates sierra check-digit. | |
| Logic based on: <https://csdirect.iii.com/sierrahelp/Content/sril/sril_records_numbers.html> """ | |
| data = str(raw_item_id).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces | |
| reversed_data = ''.join( reversed(data) ) | |
| total = 0 | |
| for counter,character in enumerate( reversed_data ): | |
| log.debug( f'character, `{character}`' ) | |
| one_index = counter + 1 | |
| log.debug( f'one_index, `{one_index}`' ) | |
| multiplier = one_index + 1 | |
| log.debug( f'multiplier, `{multiplier}`' ) | |
| multiplied = int(character) * multiplier | |
| log.debug( f'multiplied, `{multiplied}`' ) | |
| total += multiplied | |
| log.debug( f'total, `{total}`' ) | |
| check_digit = (total % 11) | |
| if check_digit == 10: | |
| check_digit = 'x' | |
| log.debug( f'check_digit, `{check_digit}`' ) | |
| return check_digit | |
| def config_log( log_level ): | |
| level_dct = { 'DEBUG': logging.DEBUG, 'INFO': logging.INFO } | |
| logging.basicConfig( | |
| level=level_dct[log_level], | |
| format='[%(asctime)s] %(levelname)s [%(module)s-%(funcName)s()::%(lineno)d] %(message)s', datefmt='%d/%b/%Y %H:%M:%S' ) | |
| if __name__ == '__main__': | |
| raw_item_number = sys.argv[1] | |
| try: | |
| log_level = sys.argv[2] | |
| except: | |
| log_level = 'INFO' | |
| config_log( log_level ) | |
| check_digit = build_check_digit( raw_item_number ) | |
| print( f'check_digit, `{check_digit}`' ) | |
| ## EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment