summaryrefslogtreecommitdiffstats
path: root/test_20121121/vonneumann.py
blob: 3e71e9d269f67da00fa6741e5deaec72d2fa31d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python

import sys

def pairs(fin):
    for c in fin.read():
        bits = bin(ord(c))[2:].zfill(8)
        while len(bits) >= 2:
            yield bits[0:2]
            bits = bits[2:]

def main(fin, fout):

    bits = ""

    for p in pairs(fin):
        if p == "11" or p == "00":
            continue
        bits += p[0]
        if len(bits) == 8:
            fout.write(chr(int(bits, 2)))
            bits = ""


if __name__=="__main__":
    if len(sys.argv) == 1:
        fin = sys.stdin
        fout = sys.stdout
    elif len(sys.argv) == 3:
        fin = open(sys.argv[1], 'r')
        fout = open(sys.argv[2], 'w')
        print "working..."
    else:
        print "ERROR: wrong number of arguments (in_file + out_file OR none)"

    main(fin, fout)
    fin.close()
    fout.close()