Last active
August 29, 2015 14:09
-
-
Save bsima/38b6a5368e7ccfab1630 to your computer and use it in GitHub Desktop.
ocr thing
This file contains 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
dictionary = { | |
' _ | ||_|': 0, | |
' | |': 1, | |
' _ _||_ ': 2, | |
' _ _| _|': 3, | |
' |_| |': 4, | |
' _ |_ _|': 5, | |
' _ |_ |_|': 6, | |
' _ | |': 7, | |
' _ |_||_|': 8, | |
' _ |_| _|': 9 | |
} | |
def parse_acct_no(input): | |
digits = get_digits(input) | |
digit_values = [] | |
for digit in digits: | |
digit_values.append(get_digit_value(digit)) | |
return tuple(digit_values) | |
def get_digits(input): | |
digits = [] | |
lines = get_lines(input) | |
for offset in range(0, 26, 3): | |
d = lines[0][offset:offset+3] | |
d += lines[1][offset:offset+3] | |
d += lines[2][offset:offset+3] | |
digits.append(d) | |
return digits | |
def get_lines(input): | |
lines = ["", "", ""] | |
offset = 0 | |
for char in input: | |
lines[offset] += char | |
if len(lines[offset]) == 27: | |
offset += 1 | |
return lines | |
def format_cell(cell): | |
return "%s\n%s\n%s" % (cell[0:3],cell[3:6],cell[6:9]) | |
def get_cell_value(cell): | |
return dictionary.get(cell, -1) | |
def get_acct_nos_from_file(filename): | |
"Reads file and get the machine-generated numbers." | |
nos = [] | |
linecount = 0 | |
no_lines = '' | |
with open(filename, 'r') as f: | |
for line in f: | |
linecount += 1 | |
if (linecount % 4) == 0: | |
nos.append(parse_acct_no(no_lines)) | |
no_lines = '' | |
else: | |
no_lines += line.rstrip('\n') | |
return nos | |
def checksum(no): | |
"""Given an account number, generates a checksum for that number. | |
(have not tested yet...)""" | |
rev = no[::-1] | |
acc = () | |
i = 0 | |
for d in rev: | |
n = d * no[i] | |
acc.append(n) | |
i += 1 | |
return acc | |
def mkreport(acct_nos): | |
"Given a collection of account numbers, generates the report." |
This file contains 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
import unittest | |
import main | |
class Tests(unittest.TestCase): | |
def test_zeros(self): | |
input = " _ _ _ _ _ _ _ _ _ "\ | |
"| || || || || || || || || |"\ | |
"|_||_||_||_||_||_||_||_||_|" | |
account_no = main.parse_acct_no(input) | |
self.assert_(account_no == (0,0,0,0,0,0,0,0,0)) | |
def test_ones(self): | |
input = " "\ | |
" | | | | | | | | |"\ | |
" | | | | | | | | |" | |
account_no = main.parse_acct_no(input) | |
self.assert_(account_no == (1,1,1,1,1,1,1,1,1)) | |
def test_get_lines(self): | |
input = " "\ | |
" | | | | | | | | |"\ | |
" | | | | | | | | |" | |
lines = main.get_lines(input) | |
self.assert_(lines[0][0] == ' ') | |
self.assert_(lines[1][1] == ' ') | |
self.assert_(lines[2][2] == '|') | |
def test_get_digits(self): | |
input = " _ _ _ _ _ _ _ _ _ "\ | |
"| || || || || || || || || |"\ | |
"|_||_||_||_||_||_||_||_||_|" | |
digits = main.get_digits(input) | |
self.assert_(main.format_digit(digits[0]) == " _ \n| |\n|_|") | |
def test_read_file(self): | |
f = 'text-parser-input.txt' | |
acct_nos = main.get_acct_nos_from_file(f) | |
self.assert_(acct_nos[10] == (1,2,3,4,5,6,7,8,9)) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment