Self-check digit calculator for UIC wagon numbers. Tools around UIC wagon numbers. Check validity of self-check digits, calculate self-check digits.
Examples:
>>> check_wagonnumber('371 015-9')
True
>>> cdigit('371 015')
9
>>> wagonnumbers('371 0??-9')
['371 001-9', '371 015-9', '371 020-9', '371 039-9', '371 044-9', '371 058-9', '371 063-9', '371 077-9', '371 082-9', '371 096-9']
Return all valid combinations of UIC wagon numbers based on the given pattern. The pattern has to be of the form “xxxxxxx-s”. The number of digits before the dash is arbitrary. “s” is the self-check digit. Any digit, including the self-check digit, may be replaced by a placeholder ”?”. Whitespace is ignored.
If fromfile is not None, filter the output by the given digit blocks found in the text file fromfile. The file must contain one block of digits per line, where the number of digits must match the number of placeholders ”?” in the wagonnumber pattern. Dashes “-” are ignored, so the file may contain lines of the form “xxx-s”.
Example usage:
>>> wagonnumbers('21 80 014 0 272-?')
['21 80 014 0 272-4']
>>> wagonnumbers('33 80 076 5 11?-?')
['33 80 076 5 110-6',
'33 80 076 5 111-4',
'33 80 076 5 112-2',
'33 80 076 5 113-0',
'33 80 076 5 114-8',
'33 80 076 5 115-5',
'33 80 076 5 116-3',
'33 80 076 5 117-1',
'33 80 076 5 118-9',
'33 80 076 5 119-7']
>>> wagonnumbers('33 80 076 5 115-4')
[]
>>> wagonnumbers('33 80 076 5 115-5')
['33 80 076 5 115-5']
>>> wagonnumbers('31 80 437 3 ???-?', fromfile='era4-white')
['31 80 437 3 300-1',
'31 80 437 3 811-7',
'31 80 437 3 217-7',
'31 80 437 3 797-8',
'31 80 437 3 354-8',
'31 80 437 3 565-9']
Background information: http://en.wikipedia.org/wiki/UIC_wagon_numbers
Check the given wagonnumber for integrity (if it has the right self-check digit). The wagon number must be of the form “xxxxxxx-x”, where each “x” stands for a digit. The number of digits before the dash is arbitrary. If the self-check digit after the dash is valid, return True, otherwise, return False.
Example usage:
>>> check_wagonnumber('120 002-2')
False
>>> check_wagonnumber('120 002-1')
True
Compute self-check digit for the given short wagon number. By short wagon number we mean one without the self-check digit (and without the dash) at the end.
Example usage:
>>> cdigit('120 002')
1
Create a wagonnumber by replacing the questionmarks ”?” in a given wagonnumber pattern by the digits given by the list comb. The number of questionmarks and the length of comb have to agree.
Note: The resulting wagon number is not checked for integrity.
Example usage:
>>> create_wagonnumber('12? 00?-1', [0, 2])
'120 002-1'
Parse given short wagon number (excluding the check digit).
Example usage:
>>> parse_short_wagonnumber('120 002')
[1, 2, 0, 0, 0, 2]
Parse given complete wagon number (including the check digit) of the form “xxxxxxx-x”. The number of digits before the dash is arbitrary.
Example usage:
>>> parse_wagonnumber('120 002-1')
([1, 2, 0, 0, 0, 2], 1)
Parse given wagon number pattern of the form “xxxxxxx-x” where each of the digits “x” may also be replaced by a questionmark ”?”. The number of digits before the dash “-” is arbitrary.
Example usage:
>>> parse_wagonnumber_pattern('120 002-1')
(7, [1, 2, 0, 0, 0, 2, 1], [0, 1, 2, 3, 4, 5, 6], [])
>>> parse_wagonnumber_pattern('120 002-?')
(7, [1, 2, 0, 0, 0, 2], [0, 1, 2, 3, 4, 5], [6])
>>> parse_wagonnumber_pattern('120 00?-?')
(7, [1, 2, 0, 0, 0], [0, 1, 2, 3, 4], [5, 6])
Compute the digit sum of the given number.
Example usage:
>>> digitsum(123)
6
Courtesy to http://stackoverflow.com/questions/14939953/sum-the-digits-of-a-number-python
Compute the total digit sum of the given list of numbers.
Example usage:
>>> total_digitsum([1, 2, 34])
10
>>> total_digitsum([1, 4, 0, 0, 0, 4])
9
(because 1+2+3+4 == 10)
Return difference of the given number to the next multiple of ten.
Example usage:
>>> diff10(1)
9
>>> diff10(8)
2
>>> diff10(10)
0
Multiply each of the given list of numbers individually from right to left alternately by 2 and 1.
Example usage:
>>> multiply21([1, 1, 1, 1])
[1, 2, 1, 2]
>>> multiply21([1, 2, 0, 0, 0, 2])
[1, 4, 0, 0, 0, 4]